嗨所以我在选择单选按钮时尝试显示一个java图像,所以我使用带有if语句的代码来识别按钮输入。但我不能让图像出现。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Color;
public class Button1 extends JPanel {
static String sadString = "Sad Joffrey";
static String happyString = "Happy Joffrey";
public Button1() {
JRadioButton sadButton = new JRadioButton(sadString);
sadButton.setMnemonic(KeyEvent.VK_B);
sadButton.setActionCommand(sadString);
sadButton.setSelected(true);
JRadioButton happyButton = new JRadioButton(happyString);
happyButton.setMnemonic(KeyEvent.VK_C);
happyButton.setActionCommand(happyString);
ButtonGroup group = new ButtonGroup();
group.add(sadButton);
group.add(happyButton);
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(sadButton);
radioPanel.add(happyButton);
class Draw extends JComponent {
public void paint(Graphics g) {
if (happyButton.isSelected()) {
g.setColor(Color.black);
g.drawRect (10, 10, 200, 200);
g.fillOval (25, 35, 50, 50);
g.fillOval (150, 35, 50, 50);
g.fillArc(75, 100, 100, 75, -200, 200);
}
}
}
add(radioPanel, BorderLayout.LINE_START);
setBorder(BorderFactory.createEmptyBorder(30,30,300,300));
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("Button1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new Button1();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
JFrame window = new JFrame();
window.getContentPane().add(new Draw());
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
我没有把悲伤的面孔画进去但是上面我想在if语句中显示代码,如果按下那个按钮,否则,它会显示悲伤的那个,那么我将如何绘制正确的代码?
答案 0 :(得分:0)
我说实话,我无法关注你的代码,所以作为对自己的挑战我从头开始。这就是我想出来的......
public class Button1 extends JPanel {
boolean sad=false;
boolean happy=false;
public void paint(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, 500, 500);
if (sad) {
g.setColor(Color.GREEN);
g.fillRect(10, 10, 50, 50);
}
if (happy) {
g.setColor(Color.black);
g.drawRect (10, 10, 200, 200);
g.fillOval (25, 35, 50, 50);
g.fillOval (150, 35, 50, 50);
g.fillArc(75, 100, 100, 75, -200, 200);
}
}
public static void main(String[] args) {
Button1 panel=new Button1();
JFrame frame=new JFrame();
frame.add(panel,BorderLayout.CENTER);
panel.addButtons(frame);
frame.setSize(500, 500);
frame.setVisible(true);
}
private void addButtons(JFrame frame) {
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
JRadioButton sadButton = new JRadioButton("Sad");
sadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
happy=false;
sad=true;
repaint();
}
});
JRadioButton happyButton = new JRadioButton("Happy");
happyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sad=false;
happy=true;
repaint();
}
});
buttonPanel.add(sadButton);
buttonPanel.add(happyButton);
ButtonGroup group = new ButtonGroup();
group.add(sadButton);
group.add(happyButton);
frame.add(buttonPanel,BorderLayout.SOUTH);
}
}