java 界面怎样实现点击一个按钮c后将Panel1以及其中的控件覆盖到panel2上
1个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RemovePanel {
private JFrame f;
private JPanel pBlue;
private JPanel pRed;
private Boolean isBlue = true;
public RemovePanel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) { }
setUIFont (new javax.swing.plaf.FontUIResource(new Font("宋体",Font.PLAIN, 80)));
System.setProperty("awt.useSystemAAFontSettings","on");
System.setProperty("swing.aatext", "true");
f = new JFrame();
pBlue = createPanel("蓝色面板", Color.blue);
pRed = createPanel("红色面板", Color.red);
f.add(pBlue, BorderLayout.CENTER);
f.add(new JButton("切换") {
{
setFocusPainted(false);
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (isBlue) {
f.remove(pBlue);
f.add(pRed, BorderLayout.CENTER);
f.revalidate();
f.repaint();
isBlue = false;
} else {
f.remove(pRed);
f.add(pBlue, BorderLayout.CENTER);
f.revalidate();
f.repaint();
isBlue = true;
}
}
});
}
}, BorderLayout.SOUTH);
f.setAlwaysOnTop(true);
f.setSize(500, 400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
private JPanel createPanel(String title, Color color) {
JPanel p = new JPanel(new BorderLayout());
p.add(new JLabel(title){
{
setHorizontalAlignment(HORIZONTAL);
}
});
p.setBackground(color);
return p;
}
private static void setUIFont(javax.swing.plaf.FontUIResource f) {
java.util.Enumeration keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof javax.swing.plaf.FontUIResource) {
UIManager.put(key, f);
}
}
}
public static void main(String[] args) {
new RemovePanel();
}
}
import java.awt.event.*;
import javax.swing.*;
public class RemovePanel {
private JFrame f;
private JPanel pBlue;
private JPanel pRed;
private Boolean isBlue = true;
public RemovePanel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) { }
setUIFont (new javax.swing.plaf.FontUIResource(new Font("宋体",Font.PLAIN, 80)));
System.setProperty("awt.useSystemAAFontSettings","on");
System.setProperty("swing.aatext", "true");
f = new JFrame();
pBlue = createPanel("蓝色面板", Color.blue);
pRed = createPanel("红色面板", Color.red);
f.add(pBlue, BorderLayout.CENTER);
f.add(new JButton("切换") {
{
setFocusPainted(false);
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (isBlue) {
f.remove(pBlue);
f.add(pRed, BorderLayout.CENTER);
f.revalidate();
f.repaint();
isBlue = false;
} else {
f.remove(pRed);
f.add(pBlue, BorderLayout.CENTER);
f.revalidate();
f.repaint();
isBlue = true;
}
}
});
}
}, BorderLayout.SOUTH);
f.setAlwaysOnTop(true);
f.setSize(500, 400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
private JPanel createPanel(String title, Color color) {
JPanel p = new JPanel(new BorderLayout());
p.add(new JLabel(title){
{
setHorizontalAlignment(HORIZONTAL);
}
});
p.setBackground(color);
return p;
}
private static void setUIFont(javax.swing.plaf.FontUIResource f) {
java.util.Enumeration keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof javax.swing.plaf.FontUIResource) {
UIManager.put(key, f);
}
}
}
public static void main(String[] args) {
new RemovePanel();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询