JAVA BUTTON事件
我在设计Java界面,点一个按钮后出现一个JPane,我想点另一个按钮,然后出现另一个Jpanel,前一个消失,如何实现,十万火急...
我在设计Java界面,点一个按钮后出现一个JPane,我想点另一个按钮,然后出现另一个Jpanel,前一个消失,如何实现,十万火急
展开
2个回答
展开全部
一个例子,楼主参考下;
把第一个panelRight.remove(firstPanel);
package baidu;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Panel1 extends JPanel {
public Panel1() {
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
add(new JButton("Test0001"), c);
c.gridx = 0;
c.gridy = 1;
add(new JButton("Test0002"), c);
c.gridx = 1;
c.gridy = 1;
add(new JButton("Test0003"), c);
}
}
class Panel2 extends JPanel {
public Panel2() {
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
add(new JButton("Try0001"), c);
c.gridx = 2;
c.gridy = 0;
add(new JButton("Try0002"), c);
c.gridx = 0;
c.gridy = 1;
add(new JButton("Try0003"), c);
}
}
public class JPanelStudy extends JFrame implements ActionListener {
private JPanel panelLeft = new JPanel();
private JPanel panelRight = new JPanel();
Panel1 firstPanel = new Panel1();
Panel2 secondPane = new Panel2();
public JPanelStudy() {
JPanel contentPane = (JPanel) getContentPane();
contentPane.add(panelLeft, BorderLayout.WEST);
contentPane.add(panelRight, BorderLayout.CENTER);
JButton button1 = new JButton("Button1");
button1.addActionListener(this);
JButton button2 = new JButton("Button2");
button2.addActionListener(this);
panelLeft.add(button1);
panelLeft.add(button2);
}
public void actionPerformed(ActionEvent event) {
JButton button = (JButton) event.getSource();
if (button.getText().equals("Button1")) {
panelRight.add(firstPanel);
panelRight.revalidate();
panelRight.setVisible(true);
} else {
System.out.println("ok");
panelRight.remove(firstPanel);
panelRight.add(secondPane);
panelRight.revalidate();
}
}
public final static void main(String[] args) {
JFrame frame = new JPanelStudy();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
把第一个panelRight.remove(firstPanel);
package baidu;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Panel1 extends JPanel {
public Panel1() {
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
add(new JButton("Test0001"), c);
c.gridx = 0;
c.gridy = 1;
add(new JButton("Test0002"), c);
c.gridx = 1;
c.gridy = 1;
add(new JButton("Test0003"), c);
}
}
class Panel2 extends JPanel {
public Panel2() {
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
add(new JButton("Try0001"), c);
c.gridx = 2;
c.gridy = 0;
add(new JButton("Try0002"), c);
c.gridx = 0;
c.gridy = 1;
add(new JButton("Try0003"), c);
}
}
public class JPanelStudy extends JFrame implements ActionListener {
private JPanel panelLeft = new JPanel();
private JPanel panelRight = new JPanel();
Panel1 firstPanel = new Panel1();
Panel2 secondPane = new Panel2();
public JPanelStudy() {
JPanel contentPane = (JPanel) getContentPane();
contentPane.add(panelLeft, BorderLayout.WEST);
contentPane.add(panelRight, BorderLayout.CENTER);
JButton button1 = new JButton("Button1");
button1.addActionListener(this);
JButton button2 = new JButton("Button2");
button2.addActionListener(this);
panelLeft.add(button1);
panelLeft.add(button2);
}
public void actionPerformed(ActionEvent event) {
JButton button = (JButton) event.getSource();
if (button.getText().equals("Button1")) {
panelRight.add(firstPanel);
panelRight.revalidate();
panelRight.setVisible(true);
} else {
System.out.println("ok");
panelRight.remove(firstPanel);
panelRight.add(secondPane);
panelRight.revalidate();
}
}
public final static void main(String[] args) {
JFrame frame = new JPanelStudy();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询