通过java中的动作侦听器刷新内容窗格

时间:2014-03-04 16:26:02

标签: java jpanel refresh actionlistener

刷新GUI的内容窗格时遇到问题。我可以创建一个执行的侦听器,但我无法访问内容窗格来更新它。到目前为止,这是我的代码:

Egutegia()和Hasiera()以及具有不同数据的JPanel的构造函数。当从开始设置为变量centralData时,它们正确加载。我的目标是在单击带有名称的按钮时相互之间进行切换。到目前为止,我只试图实现一个。

动作监听器写在后面的部分。它正确打印“到达这里”。但是,我不知道如何从那里访问内容窗格(内容)并重绘相应的JPanel。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MyApp{

private static String message1 = "message1";
private static String message2 = "message2";
private static BotMessage botMessage;
private static TopButtons topButtons;
private static JButton[] buttons;
private static JButton b1,b2,b3,b4;
public static JPanel centralData = new Hasiera();


private static class BotMessage extends JPanel {

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        double h = getHeight();
        double w = getWidth();
        Font font = new Font("default", Font.BOLD, (int)(h/2));
        g.setFont(font);
        int stw = g.getFontMetrics().stringWidth(message2); 
        g.drawString(message1, (int)(w*0.02), (int)(3*h/4));
        g.drawString(message2,(int) (w*0.98 - stw),(int)(3*h/4));
        setBackground(Color.ORANGE);
       }


 public static class TopButtons extends JPanel{
     public  TopButtons(JButton buttons[]){
        setLayout(new GridLayout(1,1));
        setBackground(Color.ORANGE);
        for (int i=0;i < buttons.length;i++){
            add(buttons[i]);
        }   
     }


 }




 /*
  *   LOADING EVERYTHING INTO A WINDOW
  */

 public static void main(String[] args) {
     botMessage = new BotMessage();      
     b1 = new JButton("Hasiera");
     ActionListener ref = new ActionListener(){
         public void actionPerformed(ActionEvent e){
             centralData = new Egutegia();
             System.out.println("reaches here");
         }
     };
     b1.addActionListener(ref);
     b2 = new JButton("Egutegia");
     b3 = new JButton("Foroa");
     b4 = new JButton("Txata"); 
     buttons = new JButton[]{b1,b2,b3,b4};
     topButtons = new TopButtons(buttons);



     JPanel content = new JPanel();
     content.setLayout(new GridBagLayout());
     GridBagConstraints c1 = new GridBagConstraints();
     c1.fill = GridBagConstraints.BOTH;
     c1.weighty = 0.02;
     c1.gridx = 0;
     c1.gridy = 0;
     c1.gridwidth = 2;
     content.add(topButtons, c1);
     GridBagConstraints c2 = new GridBagConstraints();
     c2.weightx = 1;
     c2.weighty = 1;
     c2.fill = GridBagConstraints.BOTH;
     c2.gridx = 0;
     c2.gridy = 1;
     content.add(centralData, c2);
     GridBagConstraints c3 = new GridBagConstraints();
     c3.fill = GridBagConstraints.BOTH;
     c3.weighty = 0.04;
     c3.gridx = 0;
     c3.gridy = 2;
     content.add(botMessage, c3);


     JFrame window = new JFrame("MY APP");
     window.setContentPane(content);
     window.setSize(400,600);
     window.setLocation(100,100);
     window.setVisible(true);
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    }
}

0 个答案:

没有答案