这个对于我来说太难了,可能是我太菜了 一个 JFrame 里面有四个 单选按钮 + 一个 确定按钮 + 一个标签我想利用 选择单选按钮+单击确定按钮 切换 标签的内容 ; 能实现吗?谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是部分程序:我想在切换背景色的同时来换标签lab里面的图片import java.awt.*;import javax.swing.*;import java.awt.event.*;public class test extends JFrame{ private JLabel label,lab,lab1,lab2,lab3,lab4; private Container container; private ButtonGroup radiogroup; private JRadioButton bta,btb,btc,btd; private ImageIcon icon,icon1,icon2,icon3,icon4; public test() { super("单选按钮"); container = getContentPane(); container.setLayout(new FlowLayout()); label = new JLabel("选择你最喜欢的老师:"); container.add(label); bta = new JRadioButton("张",true); bta.setOpaque(false); container.add(bta); btb = new JRadioButton("陈",false); btb.setOpaque(false); container.add(btb); btc = new JRadioButton("李",false); btc.setOpaque(false); container.add(btc); btd = new JRadioButton("吴",false); btd.setOpaque(false); container.add(btd); radiogroup = new ButtonGroup(); radiogroup.add(bta); radiogroup.add(btb); radiogroup.add(btc); radiogroup.add(btd); JButton button = new JButton("确定"); container.add(button); icon = new ImageIcon("1.jpg"); lab = new JLabel(icon); lab.setBounds(0, 0,icon.getIconWidth(), icon.getIconHeight()); container.add(lab); button.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ if(bta.isSelected()) container.setBackground(Color.RED); if(btb.isSelected()) container.setBackground(Color.GREEN); if(btc.isSelected()) container.setBackground(Color.BLUE); if(btd.isSelected()) container.setBackground(Color.YELLOW); } } ); setBounds(500,200,450,400); setVisible(true); setResizable(false); } public static void main(String[] args){ test application = new test(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }} import java.awt.*;import javax.swing.*;import java.awt.event.*;public class test extends JFrame { private JLabel label, lab, lab1, lab2, lab3, lab4; private Container container; private ButtonGroup radiogroup; private JRadioButton bta, btb, btc, btd; private ImageIcon icon, icon1, icon2, icon3, icon4; public test() { super("单选按钮"); container = getContentPane(); container.setLayout(new FlowLayout()); label = new JLabel("选择你最喜欢的老师:"); container.add(label); bta = new JRadioButton("张", true); bta.setOpaque(false); container.add(bta); btb = new JRadioButton("陈", false); btb.setOpaque(false); container.add(btb); btc = new JRadioButton("李", false); btc.setOpaque(false); container.add(btc); btd = new JRadioButton("吴", false); btd.setOpaque(false); container.add(btd); radiogroup = new ButtonGroup(); radiogroup.add(bta); radiogroup.add(btb); radiogroup.add(btc); radiogroup.add(btd); JButton button = new JButton("确定"); container.add(button); icon1 = new ImageIcon("1.jpg"); icon2 = new ImageIcon("2.jpg"); icon3 = new ImageIcon("3.jpg"); icon4 = new ImageIcon("4.jpg"); lab = new JLabel(); container.add(lab); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (bta.isSelected()) { container.setBackground(Color.RED); lab.setIcon(icon1); } if (btb.isSelected()) { container.setBackground(Color.GREEN); lab.setIcon(icon2); } if (btc.isSelected()) { container.setBackground(Color.BLUE); lab.setIcon(icon3); } if (btd.isSelected()) { container.setBackground(Color.YELLOW); lab.setIcon(icon4); } if (lab.getIcon() != null) { lab.setBounds(0, 0, lab.getIcon().getIconWidth(), lab.getIcon().getIconHeight()); } } }); setBounds(500, 200, 450, 400); setVisible(true); setResizable(false); } public static void main(String[] args) { test application = new test(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }} 图片位置不对我修改了一下if (lab.getIcon() != null) { lab.setSize(lab.getIcon().getIconWidth(), lab.getIcon().getIconHeight());这样就好了 关于JPanel响应键盘事件的问题!高分求解!在线等待 Java Web开发方式讨论 如何新建立一個struts工程 100分求解GUI问题 java如何抽析图象制作缩略图? 请问如何在frame上画图? Method是String类型,值是一个函数的方法名,如何实现这个方法的调用? 初学者请教JDBC的问题 请教各位大虾,如何用java读取一个多行CSV文件???(高分求助,不够再加) 关于使用数据库连接的问题。 java数据库查询问题 求大神指点 List组件挡住了菜单 怎么办啊?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class test extends JFrame{
private JLabel label,lab,lab1,lab2,lab3,lab4;
private Container container;
private ButtonGroup radiogroup;
private JRadioButton bta,btb,btc,btd;
private ImageIcon icon,icon1,icon2,icon3,icon4; public test() {
super("单选按钮");
container = getContentPane();
container.setLayout(new FlowLayout()); label = new JLabel("选择你最喜欢的老师:");
container.add(label); bta = new JRadioButton("张",true);
bta.setOpaque(false);
container.add(bta); btb = new JRadioButton("陈",false);
btb.setOpaque(false);
container.add(btb); btc = new JRadioButton("李",false);
btc.setOpaque(false);
container.add(btc); btd = new JRadioButton("吴",false);
btd.setOpaque(false);
container.add(btd); radiogroup = new ButtonGroup();
radiogroup.add(bta);
radiogroup.add(btb);
radiogroup.add(btc);
radiogroup.add(btd); JButton button = new JButton("确定");
container.add(button); icon = new ImageIcon("1.jpg");
lab = new JLabel(icon);
lab.setBounds(0, 0,icon.getIconWidth(), icon.getIconHeight());
container.add(lab); button.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if(bta.isSelected()) container.setBackground(Color.RED);
if(btb.isSelected()) container.setBackground(Color.GREEN);
if(btc.isSelected()) container.setBackground(Color.BLUE);
if(btd.isSelected()) container.setBackground(Color.YELLOW);
}
}
);
setBounds(500,200,450,400);
setVisible(true);
setResizable(false);
}
public static void main(String[] args){
test application = new test();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import javax.swing.*;
import java.awt.event.*;public class test extends JFrame {
private JLabel label, lab, lab1, lab2, lab3, lab4;
private Container container;
private ButtonGroup radiogroup;
private JRadioButton bta, btb, btc, btd;
private ImageIcon icon, icon1, icon2, icon3, icon4; public test() {
super("单选按钮");
container = getContentPane();
container.setLayout(new FlowLayout()); label = new JLabel("选择你最喜欢的老师:");
container.add(label); bta = new JRadioButton("张", true);
bta.setOpaque(false);
container.add(bta); btb = new JRadioButton("陈", false);
btb.setOpaque(false);
container.add(btb); btc = new JRadioButton("李", false);
btc.setOpaque(false);
container.add(btc); btd = new JRadioButton("吴", false);
btd.setOpaque(false);
container.add(btd); radiogroup = new ButtonGroup();
radiogroup.add(bta);
radiogroup.add(btb);
radiogroup.add(btc);
radiogroup.add(btd); JButton button = new JButton("确定");
container.add(button); icon1 = new ImageIcon("1.jpg");
icon2 = new ImageIcon("2.jpg");
icon3 = new ImageIcon("3.jpg");
icon4 = new ImageIcon("4.jpg");
lab = new JLabel();
container.add(lab); button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (bta.isSelected()) {
container.setBackground(Color.RED);
lab.setIcon(icon1);
}
if (btb.isSelected()) {
container.setBackground(Color.GREEN);
lab.setIcon(icon2);
}
if (btc.isSelected()) {
container.setBackground(Color.BLUE);
lab.setIcon(icon3);
}
if (btd.isSelected()) {
container.setBackground(Color.YELLOW);
lab.setIcon(icon4);
}
if (lab.getIcon() != null) {
lab.setBounds(0, 0, lab.getIcon().getIconWidth(), lab.getIcon().getIconHeight());
}
}
}); setBounds(500, 200, 450, 400);
setVisible(true);
setResizable(false);
} public static void main(String[] args) {
test application = new test();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
我修改了一下
if (lab.getIcon() != null) {
lab.setSize(lab.getIcon().getIconWidth(), lab.getIcon().getIconHeight());
这样就好了