向 JFrame 添加多个按钮,文本+颜色未显示
Adding multiple buttons to JFrame, text+color not showing up
我正在尝试将 10 个按钮一个一个地添加到 JPanel
,然后将它们全部添加到 JFrame
。我需要有我的 for 循环,因为它必须很容易改变按钮的数量。所有的按钮也需要有不同的颜色和文本(我知道它们可以用下面的代码得到相同的颜色,但现在没问题)。
我从下面的代码输出的只是一个有 10 个白色按钮的框架,没有 text/color。为什么 colors
、text
和 actionlistener
没有连接到我的按钮?
我读过关于 frame.add(panel
、frame.pack()
和 frame.setVisible(true)
的其他问题,我必须注意我的位置,但我认为这些正确地放置在我的之外forloop
。我也尝试使用 frame.setContentPane(panel)
,但这给出了相同的结果 - 一个有 10 个白色按钮但没有 text/color.
的框架
CMain class
:
public class CMain extends MyButton {
public static void main(String[] args) {
Random randomGenerator = new Random();
int numberofbuttons = 10;
JPanel panel = new JPanel();
JFrame frame = new JFrame("MyButton testing");
for (int i = 0; i < numberofbuttons; i++) {
float r = randomGenerator.nextFloat();float g = randomGenerator.nextFloat();float b = randomGenerator.nextFloat();float r2 = randomGenerator.nextFloat();float g2 = randomGenerator.nextFloat();float b2 = randomGenerator.nextFloat();
String theText = "SWITCH ME BACK";
String theOtherText = "button nr: " + i;
Color theColor = new Color(r, g, b);
Color theOtherColor = new Color(r2, g2, b2);
MyButton myb = new MyButton(theColor, theOtherColor, theText, theOtherText);
panel.add(myb);
}
frame.add(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
MyButton class
:
public class MyButton extends JButton implements ActionListener {
private JButton button;
private Color col1;
private Color col2;
private String text1;
private String text2;
public MyButton(Color col1, Color col2, String text1, String text2) {
this.col1 = col1;
this.col2 = col2;
this.text1 = text1;
this.text2 = text2;
button = new JButton(text1);
button.setOpaque(true);
button.setBackground(col1);
button.addActionListener(this);
}
public MyButton() {
this(Color.blue, Color.red, "click = make red", "click = reset to blue");
}
public void ToggleState() {
Color initialBackground = button.getBackground();
if (initialBackground == col1) {
button.setBackground(col2);
button.setText(text2);
} else if (initialBackground == col2) {
button.setBackground(col1);
button.setText(text1);
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
this.ToggleState();
}
}
}
您正在使用 class 创建两个 JButton,一个是 class 本身的对象(this
,如果您愿意的话),第二个是 button
JButton
内变量 class:
public class MyButton extends JButton implements ActionListener { // **** this is a JButton
private JButton button; // ***** and so is THIS!
摆脱 button
变量,只使用 this
,您的问题可能会得到解决。
public class MyButton extends JButton implements ActionListener { // this is a JButton
// private JButton button; // get rid of this
// and then change all code where you try to use button to this.
例如,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyButton extends JButton implements ActionListener {
// private JButton button;
private Color col1;
private Color col2;
private String text1;
private String text2;
public MyButton(Color col1, Color col2, String text1, String text2) {
super(text1); // *********** also add this **********
this.col1 = col1;
this.col2 = col2;
this.text1 = text1;
this.text2 = text2;
// button = new JButton(text1);
this.setOpaque(true);
this.setBackground(col1);
this.addActionListener(this);
}
public MyButton() {
this(Color.blue, Color.red, "click = make red", "click = reset to blue");
}
public void ToggleState() {
Color initialBackground = this.getBackground();
if (initialBackground == col1) {
this.setBackground(col2);
this.setText(text2);
} else if (initialBackground == col2) {
this.setBackground(col1);
this.setText(text1);
}
}
public void actionPerformed(ActionEvent e) {
// if (e.getSource() == button) {
this.ToggleState();
// }
}
}
我正在尝试将 10 个按钮一个一个地添加到 JPanel
,然后将它们全部添加到 JFrame
。我需要有我的 for 循环,因为它必须很容易改变按钮的数量。所有的按钮也需要有不同的颜色和文本(我知道它们可以用下面的代码得到相同的颜色,但现在没问题)。
我从下面的代码输出的只是一个有 10 个白色按钮的框架,没有 text/color。为什么 colors
、text
和 actionlistener
没有连接到我的按钮?
我读过关于 frame.add(panel
、frame.pack()
和 frame.setVisible(true)
的其他问题,我必须注意我的位置,但我认为这些正确地放置在我的之外forloop
。我也尝试使用 frame.setContentPane(panel)
,但这给出了相同的结果 - 一个有 10 个白色按钮但没有 text/color.
CMain class
:
public class CMain extends MyButton {
public static void main(String[] args) {
Random randomGenerator = new Random();
int numberofbuttons = 10;
JPanel panel = new JPanel();
JFrame frame = new JFrame("MyButton testing");
for (int i = 0; i < numberofbuttons; i++) {
float r = randomGenerator.nextFloat();float g = randomGenerator.nextFloat();float b = randomGenerator.nextFloat();float r2 = randomGenerator.nextFloat();float g2 = randomGenerator.nextFloat();float b2 = randomGenerator.nextFloat();
String theText = "SWITCH ME BACK";
String theOtherText = "button nr: " + i;
Color theColor = new Color(r, g, b);
Color theOtherColor = new Color(r2, g2, b2);
MyButton myb = new MyButton(theColor, theOtherColor, theText, theOtherText);
panel.add(myb);
}
frame.add(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
MyButton class
:
public class MyButton extends JButton implements ActionListener {
private JButton button;
private Color col1;
private Color col2;
private String text1;
private String text2;
public MyButton(Color col1, Color col2, String text1, String text2) {
this.col1 = col1;
this.col2 = col2;
this.text1 = text1;
this.text2 = text2;
button = new JButton(text1);
button.setOpaque(true);
button.setBackground(col1);
button.addActionListener(this);
}
public MyButton() {
this(Color.blue, Color.red, "click = make red", "click = reset to blue");
}
public void ToggleState() {
Color initialBackground = button.getBackground();
if (initialBackground == col1) {
button.setBackground(col2);
button.setText(text2);
} else if (initialBackground == col2) {
button.setBackground(col1);
button.setText(text1);
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
this.ToggleState();
}
}
}
您正在使用 class 创建两个 JButton,一个是 class 本身的对象(this
,如果您愿意的话),第二个是 button
JButton
内变量 class:
public class MyButton extends JButton implements ActionListener { // **** this is a JButton
private JButton button; // ***** and so is THIS!
摆脱 button
变量,只使用 this
,您的问题可能会得到解决。
public class MyButton extends JButton implements ActionListener { // this is a JButton
// private JButton button; // get rid of this
// and then change all code where you try to use button to this.
例如,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyButton extends JButton implements ActionListener {
// private JButton button;
private Color col1;
private Color col2;
private String text1;
private String text2;
public MyButton(Color col1, Color col2, String text1, String text2) {
super(text1); // *********** also add this **********
this.col1 = col1;
this.col2 = col2;
this.text1 = text1;
this.text2 = text2;
// button = new JButton(text1);
this.setOpaque(true);
this.setBackground(col1);
this.addActionListener(this);
}
public MyButton() {
this(Color.blue, Color.red, "click = make red", "click = reset to blue");
}
public void ToggleState() {
Color initialBackground = this.getBackground();
if (initialBackground == col1) {
this.setBackground(col2);
this.setText(text2);
} else if (initialBackground == col2) {
this.setBackground(col1);
this.setText(text1);
}
}
public void actionPerformed(ActionEvent e) {
// if (e.getSource() == button) {
this.ToggleState();
// }
}
}