通过java中的操作侦听器刷新内容窗格
刷新GUI的内容窗格时出现问题。我可以创建一个已执行的侦听器,但无法访问内容窗格来更新它。以下是我目前的代码: Egutegia()和Hasiera()以及具有不同数据的JPanel的构造函数。当从数据库中设置为变量centralData时,它们将正确加载。我的目标是在单击带有名称的按钮时在彼此之间切换。到目前为止,我只尝试实现了一个 动作侦听器将在后面的部分中编写。它正确地打印“到达这里”。但是,我不知道如何从那里访问内容窗格(content)并重新绘制相应的JPanel通过java中的操作侦听器刷新内容窗格,java,jpanel,refresh,actionlistener,Java,Jpanel,Refresh,Actionlistener,刷新GUI的内容窗格时出现问题。我可以创建一个已执行的侦听器,但无法访问内容窗格来更新它。以下是我目前的代码: Egutegia()和Hasiera()以及具有不同数据的JPanel的构造函数。当从数据库中设置为变量centralData时,它们将正确加载。我的目标是在单击带有名称的按钮时在彼此之间切换。到目前为止,我只尝试实现了一个 动作侦听器将在后面的部分中编写。它正确地打印“到达这里”。但是,我不知道如何从那里访问内容窗格(content)并重新绘制相应的JPanel import jav
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);
}
}
import java.awt.*;
导入javax.swing.*;
导入java.awt.event.*;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
公共类MyApp{
私有静态字符串message1=“message1”;
私有静态字符串message2=“message2”;
私有静态BotMessage;
私人静态按钮;
私有静态JButton[]按钮;
专用静态按钮b1、b2、b3、b4;
public static JPanel centralData=new Hasiera();
私有静态类BotMessage扩展了JPanel{
公共组件(图形g){
超级组件(g);
双h=getHeight();
双w=getWidth();
Font Font=新字体(“默认”,Font.BOLD,(int)(h/2));
g、 setFont(字体);
int stw=g.getFontMetrics().stringWidth(message2);
g、 抽绳(message1,(int)(w*0.02),(int)(3*h/4));
g、 抽绳(信息2,(内部)(w*0.98-stw),(内部)(3*h/4));
挫折背景(橙色);
}
公共静态类TopButtons扩展了JPanel{
公共顶部按钮(JButton按钮[]){
setLayout(新的GridLayout(1,1));
挫折背景(橙色);
对于(int i=0;i
Nevermind,我解决了这个问题。我需要在myApp类的Begging中,而不是在main类中,将内容声明为静态变量来访问它。