我想做这样的事情。
主班
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JButton;
public class test
{
public static JFrame frame;
public static JPanel panel;
public static JButton settings;
public static int red = 10;
public static int green = 100;
public static int blue = 10;
public static void main( String[] args )
{
frame = new JFrame();
frame.setSize( 100 , 100 );
panel = new JPanel();
panel.setBackground( new Color( red , green , blue) );
frame.add( panel );
settings = new JButton( "settings" );
panel.add( settings );
settings.addActionListener( new settingsctionListener() );
frame.setVisible( true );
}
}
另一个班级
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class settingsctionListener implements ActionListener
{
@Override
public void actionPerformed( ActionEvent e )
{
test.red = 100;
test.green = 255;
test.blue = 255;
}
}
不知道该怎么做。
另一个类应该将变量值更改为红色,绿色和蓝色,以更改整个主题。
有一个方法panel.setBackground(...),但这只会更改面板颜色。喜欢更改整个主题的变量。
答案 0 :(得分:0)
red
,green
和blue
时,面板和其他元素不会自动更新。它们需要使用各自的“设置”方法分别进行更新。JPanel
,这是updateTheme
中唯一更新的,但是您可能需要添加其他setter调用。import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JButton;
public class EntryPoint {
public static JFrame frame;
public static JPanel panel;
public static JButton settings;
public static int red = 10;
public static int green = 100;
public static int blue = 10;
public static void updateTheme(int r, int g, int b) {
panel.setBackground( new Color(r, g, b) );
// Update any other UI elements (panels etc).
// Update the static variables in case they're needed for some reason
red = r;
green = g;
blue = b;
}
public static void main( String[] args )
{
frame = new JFrame();
frame.setSize( 100 , 100 );
panel = new JPanel();
panel.setBackground( new Color( red , green , blue) );
frame.add( panel );
settings = new JButton( "settings" );
panel.add( settings );
settings.addActionListener( new SettingsActionListener() );
frame.setVisible( true );
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SettingsActionListener implements ActionListener
{
// Do not need Override if using ActionListener trait
public void actionPerformed( ActionEvent e )
{
// invoke the static updateTheme method
EntryPoint.updateTheme(100, 255, 255);
System.out.println("Settings updated");
}
}
答案 1 :(得分:0)
在找到最终解决方案之前,我们应该对您的代码进行一些整理。特别是
考虑了这一点之后,您可以采用四种方法来实现ActionListener。 Swing开发中的一种常见方法是使用匿名类。然后它将如下所示:
class SwingTest1 extends JFrame {
final JPanel panel;
final JButton settings;
int red = 10;
int green = 100;
int blue = 10;
private SwingTest1() {
panel = new JPanel();
panel.setBackground(new Color(red, green, blue));
add(panel);
settings = new JButton("settings");
panel.add(settings);
// anonymous ActionListener class
settings.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
red = 100;
green = 255;
blue = 255;
panel.setBackground(new Color(red, green, blue));
}
});
}
public static void main(String[] args) {
SwingTest1 frame = new SwingTest1();
frame.setSize(100, 100);
frame.setVisible(true);
}
}
或者您也可以使您的类也实现ActionListener:
class SwingTest2 extends JFrame implements ActionListener {
final JPanel panel;
final JButton settings;
int red = 10;
int green = 100;
int blue = 10;
private SwingTest2() {
panel = new JPanel();
panel.setBackground(new Color(red, green, blue));
add(panel);
settings = new JButton("settings");
panel.add(settings);
// ActionListener as self-reference
settings.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
this.red = 100;
this.green = 255;
this.blue = 255;
panel.setBackground(new Color(red, green, blue));
}
public static void main(String[] args) {
SwingTest2 frame = new SwingTest2();
frame.setSize(100, 100);
frame.setVisible(true);
}
}
如果您真的想拥有两个类,请对框架进行反向引用:
class SwingTest3 extends JFrame {
final JPanel panel;
final JButton settings;
int red = 10;
int green = 100;
int blue = 10;
private SwingTest3() {
panel = new JPanel();
panel.setBackground(new Color(100, 100, 10));
add(panel);
settings = new JButton("settings");
panel.add(settings);
// ActionListener with back reference
settings.addActionListener(new SettingsListener(this));
}
public void setColor(int red, int green, int blue) {
this.red = red;
this.green = green;
this.blue = blue;
panel.setBackground(new Color(red, green, blue));
}
public static void main(String[] args) {
SwingTest3 frame = new SwingTest3();
frame.setSize(100, 100);
frame.setVisible(true);
}
}
和Listener类:
class SettingsListener implements ActionListener {
final SwingTest3 frame;
public SettingsListener(SwingTest3 frame) {
super();
this.frame = frame;
}
@Override
public void actionPerformed(ActionEvent e) {
frame.setColor(100, 255, 255);
}
}
从Java 8开始,您还可以将Lamda表达式用作ActionListener。这使您的代码更紧凑,但对于初学者来说可能更难阅读:
class SwingTest4 extends JFrame {
final JPanel panel;
final JButton settings;
private SwingTest4() {
panel = new JPanel();
panel.setBackground(new Color(100, 100, 10));
add(panel);
settings = new JButton("settings");
panel.add(settings);
// Lambda style ActionListener
settings.addActionListener(e -> panel.setBackground(new Color(100, 255, 255)));
}
public static void main(String[] args) {
SwingTest4 frame = new SwingTest4();
frame.setSize(100, 100);
frame.setVisible(true);
}
}
现在由您决定最喜欢哪种解决方案。