[英]Java swing questions in ActionListener
//GUI.java
public class GUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 870343916997182570L;
private JPanel btmPanel;
public GUI(String arg0) throws HeadlessException {
super(arg0);
createGUI();
}
private void createGUI() {
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
//ResultPanel rslt = new ResultPanel();
//this.getContentPane().add(rslt.createPanel(), BorderLayout.CENTER);
btmPanel = new JPanel();
btmPanel.setBackground(Color.LIGHT_GRAY);
btmPanel.setLayout(new FlowLayout());
JButton blueSearch = new JButton("Search");
blueSearch.setBackground(Color.WHITE);
blueSearch.addActionListener(this);
btmPanel.add(blueSearch);
JButton blackChart = new JButton("Chart");
blackChart.setBackground(Color.WHITE);
blackChart.addActionListener(this);
btmPanel.add(blackChart);
this.getContentPane().add(btmPanel, BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e) {
String buttonString = e.getActionCommand();
if (buttonString.equals("Search")) {
ResultPanel rslt = new ResultPanel();
this.getContentPane().add(rslt.createPanel(), BorderLayout.CENTER);
}
}
}
//ResultPanel.java
public class ResultPanel extends JPanel implements ActionListener {
private static final long serialVersionUID = -7851502165390304971L;
private JPanel textPanel;
private JTextArea textDisplay;
public ResultPanel() {
textPanel = new JPanel();
textDisplay = new JTextArea("Text Area:");
}
public JPanel createPanel() {
textDisplay.setEditable(true);
textPanel.setBackground(Color.LIGHT_GRAY);
textPanel.setLayout(new BorderLayout());
textPanel.add(textDisplay,BorderLayout.CENTER);
return textPanel;
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
我在主機上有兩個按鈕,我希望在按下按鈕時更換面板。
問題是“ actionPerformed”中的代碼不起作用,
但是,如果我將它們放在creatGUI()中,效果很好。...(請參閱標記部分)。
那有什么事嗎?
在運行中添加組件之后,需要將revalidate()
和repaint()
調用到JFrame
,以使其可見。
盡管如您所說,如果您將它們添加到createGUI()
,就可以看到它,因為那時它是對Swing應用程序的靜態添加,因此您首先添加了它,然后將其設置為visible。
盡管您的代碼有一些漏洞,但我可以很快告訴您的是,您擴展了JPanel
for ResultPanel
,盡管您從未使用過ResultPanel
,所以我修改了代碼以將ResultPanel
納入透視圖。 在這里嘗試從您的示例修改后的代碼:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AddComponentExample
{
private JFrame frame;
private JPanel btmPanel;
private ResultPanel resultPanel;
public AddComponentExample()
{
resultPanel = new ResultPanel();
}
private void display()
{
frame = new JFrame("Adding Component Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btmPanel = new JPanel();
btmPanel.setBackground(Color.LIGHT_GRAY);
btmPanel.setLayout(new FlowLayout());
JButton blueSearch = new JButton("Search");
blueSearch.setBackground(Color.WHITE);
blueSearch.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (!resultPanel.isShowing())
{
resultPanel = resultPanel.createPanel();
frame.getContentPane().add(resultPanel, BorderLayout.CENTER);
frame.revalidate(); // For Java 7 and above.
// frame.getContentPane().revalidate(); // For Java 1.6 or below.
frame.repaint(); // required sometimes
}
else
System.out.println("Panel is already Visible");
}
});
btmPanel.add(blueSearch);
JButton blackChart = new JButton("Chart");
blackChart.setBackground(Color.WHITE);
//blackChart.addActionListener(this);
btmPanel.add(blackChart);
frame.getContentPane().add(btmPanel, BorderLayout.PAGE_END);
frame.setSize(500, 500);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new AddComponentExample().display();
}
});
}
}
class ResultPanel extends JPanel implements ActionListener {
private static final long serialVersionUID = -7851502165390304971L;
private JPanel textPanel;
private JTextArea textDisplay;
public ResultPanel() {
textPanel = new JPanel();
textDisplay = new JTextArea("Text Area:");
}
public ResultPanel createPanel() {
textDisplay.setEditable(true);
textPanel.setBackground(Color.LIGHT_GRAY);
textPanel.setLayout(new BorderLayout());
textPanel.add(textDisplay,BorderLayout.CENTER);
add(textPanel);
return this;
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
只需調用pack();
添加面板后。 這將使JFrame顯示更新。
if (buttonString.equals("Search")) {
ResultPanel rslt = new ResultPanel();
this.getContentPane().add(rslt.createPanel(), BorderLayout.CENTER);
pack();
}
使用ActionListener的 匿名類
Use one line of code: this.revalidate();
This will validate and repaint the frame so that it can show the JPanel.
if (buttonString.equals("Search")) {
ResultPanel rslt = new ResultPanel();
this.getContentPane().add(rslt.createPanel(), BorderLayout.CENTER);
this.revalidate();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.