set a icon for JRadioButton

liaomingxue 2003-10-04 10:42:14
Sorry,Chinese Input failed!!!!!!!!!!!

Set a icon for a JRadioButton component meeting the following:
1: a icon including
2: text excluded
and keeing radiobutton's original round check box to indicate whether checked or not.

High score is available for good answer
...全文
51 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
liad 2003-10-07
  • 打赏
  • 举报
回复
JToolBar toolBar = new JToolBar();
toolBar.setLayout(new FlowLayout(FlowLayout.LEFT));
// toolBar.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 0));
liaomingxue 2003-10-06
  • 打赏
  • 举报
回复
提前
raysand 2003-10-06
  • 打赏
  • 举报
回复
<![CDATA[
Sorry,Chinese Input failed!!!!!!!!!!!

Set a icon for a JRadioButton component meeting the following:
1: a icon including
2: text excluded
and keeing radiobutton's original round check box to indicate whether checked or not.

High score is available for good answer
]]>
liaomingxue 2003-10-06
  • 打赏
  • 举报
回复
问题是这样的:
radioButton.setText("<html><img src=a image file name>");
可以保留原来的圆形按钮,并将我们的图标放到后面,但是问题是:
我们的这个图标将尽可能地占据剩余空间.如果将这个按钮与其他普通按钮
加到一个工具栏中,我们再最大化窗口,就会发现这个按钮占据了庞大的空间
宽度!!!!!!这是为什么呢?这个按钮变得象glue一样!!!
newman0708 2003-10-05
  • 打赏
  • 举报
回复
对这个网页上有,看过了。
http://javaalmanac.com/egs/javax.swing/checkbox_CustIcon.html
liaomingxue 2003-10-05
  • 打赏
  • 举报
回复
输入法好了,我重新说一下:

按照liad,可以放置两个图标,一个是原来的,用来指示选择状态;
一个是我们配置的,用来显示按钮含义;但是我们定义的图标将占据
很宽的空间,不管怎样设置大小,都无法避免.

按照后面两者的说法,只能放一个图标.
liaomingxue 2003-10-04
  • 打赏
  • 举报
回复
according to liad, the icon we add will hold a very wide space
even if setting a width property for img,td,or tr html tag.
Also, I use img tag directly,but the same result.....

for YuLimin(阿敏当兵) :
the method tells us how to change icon ,not to set a new icon
together with default round icon.
YuLimin 2003-10-04
  • 打赏
  • 举报
回复
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JRadioButton;

public class Test extends JApplet
{
public void init()
{
Container contentPane = getContentPane();
Icon icon = new ImageIcon(getImage(getCodeBase(),"bulb.gif"));
JRadioButton[] radioButtons = new JRadioButton[]
{
new JRadioButton(),
new JRadioButton(icon),
new JRadioButton(icon,true),
new JRadioButton("idea!"),
new JRadioButton("idea!",true),
new JRadioButton("idea!",icon),
new JRadioButton("idea!",icon,true)
};
contentPane.setLayout(new FlowLayout());

for(int i = 0;i < radioButtons.length;++i)
{
radioButtons[i].setBorderPainted(true);
contentPane.add(radioButtons[i]);

if(radioButtons[i].getIcon() != null)
{
System.out.println("setting selected icon");
radioButtons[i].setSelectedIcon(new ImageIcon(getImage(getCodeBase(),"bulb_bright.gif")));
}
}
}
}
liad 2003-10-04
  • 打赏
  • 举报
回复
this maybe help
http://javaalmanac.com/egs/javax.swing/pkg.html#JRadioButton
手工写的旅店管理系统界面。 import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.util.HashMap; import java.util.Iterator; import java.util.Set; import javax.swing.ButtonGroup; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JRadioButton; import javax.swing.JTextField; public class MyButton extends JButton implements MouseListener, ActionListener { private static final long serialVersionUID = 6261021704484837560L; private JPopupMenu popMenu; private ButtonGroup bg; private JRadioButton SetDark;// 置为脏房,待打扫 private JRadioButton SetClean;// 置为应该打扫的房间 private JRadioButton SetUsing;// 置为正在使用的房间 private JRadioButton SetHouseNum;// 设置房号 private JRadioButton SetEmpty;//置为空房 private JRadioButton Delete;//删除 private JRadioButton Bujiaoyajin;//补交押金 private String HouseAr, HouseNu; private Color Housecolor; private int ID; private double SingelSal; public double getSingelSal() { return SingelSal; } public void setSingelSal(double singelSal) { SingelSal = singelSal; } private HashMap tempMap; private Customer customer; public MyButton(int ID,HashMap tempMap) { super.setIcon(new ImageIcon("Blue Victorian.JPG")); this.ID = ID; this.tempMap = tempMap; init(); } public MyButton() { super.setIcon(new ImageIcon("Blue Victorian.JPG")); init(); } public void init() { Housecolor = Color.GREEN; super.setBackground(Housecolor); popMenu = new JPopupMenu(); bg = new ButtonGroup(); SetDark = new JRadioButton("置为脏房"); SetClean = new JRadioButton("置为闲置房"); SetUsing = new JRadioButton("添加留客"); SetHouseNum = new JRadioButton("初始房态"); SetEmpty = new JRadioButton("置为空房"); Delete = new JRadioButton("删除本房"); Bujiaoyajin = new JRadioButton("补交押金"); // 添加action监听 SetDark.addActionListener(this); SetClean.addActionListener(this); SetUsing.addActionListener(this); SetHouseNum.addActionListener(this); SetEmpty.addActionListener(this); Delete.addActionListener(this); Bujiaoyajin.addActionListener(this); // 加入buttongroup,实现单选 bg.add(SetClean); bg.add(SetDark); bg.add(SetUsing); bg.add(SetHouseNum); bg.add(SetEmpty); bg.add(Delete); bg.add(Bujiaoyajin); //bg.add(Delete); // 设置相应房态颜色 popMenu.add(SetDark); popMenu.add(SetClean); popMenu.add(SetUsing); popMenu.add(SetHouseNum); popMenu.add(SetEmpty); popMenu.add(Bujiaoyajin); //popMenu.add(Delete); // 设置jbutton的固定大小 setMaximumSize(new Dimension(80, 60)); setMinimumSize(new Dimension(80, 60)); setPreferredSize(new Dimension(80, 60)); this.addMouseListener(this); } public void mouseClicked(MouseEvent arg0) {} public void mouseEntered(MouseEvent arg0) {} public void mouseExited(MouseEvent arg0) {} public void mouseReleased(MouseEvent arg0) {} // 为jbuuton添加popmenu public void mousePressed(MouseEvent arg0) { if (arg0.getButton() == MouseEvent.BUTTON3) { popMenu.show(arg0.getComponent(), arg0.getX(), arg0.getY()); } } //定位该button在map中的位置 public MyButton indexof() { MyButton b = null; Set set = this.tempMap.keySet(); Iterator iter = set.iterator(); while(iter.hasNext()) { String id = iter.next(); if(this.ID == Integer.parseInt(id.trim())) b = this.tempMap.get(id); } return b; } //getcustomer的信息 public Customer getCustomer() { return this.customer; } // 为选项添加监听 public void actionPerformed(ActionEvent e) { if(e.getSource() == Bujiaoyajin) { System.out.println("fgfhjhk"); } if (e.getSource() == SetDark) { Housecolor = Color.yellow; this.setBackground(Housecolor); indexof().setHouseColor(Housecolor); Write(); } if (e.getSource() == SetClean) { Housecolor = Color.gray; this.setBackground(Housecolor); indexof().setHouseColor(Housecolor); Write(); } if (e.getSource() == Delete) { tempMap.remove(ID); Write(); } if (e.getSource() == SetEmpty) { Housecolor = Color.green; this.setBackground(Housecolor); indexof().setHouseColor(Housecolor); Write(); } if (e.getSource() == SetUsing) { new GetCustomerInfo(); this.setBackground(Housecolor); } if (e.getSource() == SetHouseNum) { new GetHouseInfo(); } } public int getID() { return ID; } public void setID(int id) { ID = id; } //保存到本地 public void Write() { try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:/ButtonsInfo.shf")); oos.writeObject(this.tempMap); oos.flush(); oos.close(); } catch (Exception e) { e.printStackTrace(); } } //为外层程序提供 public void setHouseAr(String str) { this.HouseAr = str; } public String getHouseAr() { return this.HouseAr; } public void setHouseNu(String str) { this.HouseNu = str; } public String getHouseNu() { return this.HouseNu; } public void setHouseColor(Color cl) { this.Housecolor = cl; } public Color getHouseColor() { return this.Housecolor; } // 设置房号的窗口 class GetHouseInfo extends JFrame implements ActionListener { private static final long serialVersionUID = 4824740927444846504L; private JPanel p, p1, p2, p3,p4; private JLabel HouseArea, HouseNum,SingleSal; private JTextField HouseA, HouseN,SinglS; private JButton Entrue, Cansal; private String A,N; public GetHouseInfo() { super.setTitle("初始房态"); p = new JPanel(); p1 = new JPanel(); p2 = new JPanel(); p3 = new JPanel(); p4 = new JPanel(); HouseArea = new JLabel("设定房区:"); HouseNum = new JLabel("设定房号:"); SingleSal = new JLabel("设定单价:"); HouseA = new JTextField(10); HouseN = new JTextField(10); SinglS = new JTextField(10); Entrue = new JButton("确定"); Cansal = new JButton("取消"); Entrue.addActionListener(this); Cansal.addActionListener(this); p.setLayout(new GridLayout(4, 1)); p1.add(HouseArea); p1.add(HouseA); p2.add(HouseNum); p2.add(HouseN); p3.add(Entrue); p3.add(Cansal); p4.add(SingleSal); p4.add(SinglS); p.add(p1); p.add(p2); p.add(p4); p.add(p3); this.add(p); this.setLocation(450, 300); this.setAlwaysOnTop(true); this.setVisible(true); this.pack(); } public String getHouseA() { return this.A; } public String getHouseN() { return this.N; } @SuppressWarnings("deprecation") public void actionPerformed(ActionEvent arg0) { if (arg0.getSource() == Entrue) { A = HouseAr = HouseA.getText(); N = HouseNu = HouseN.getText(); SingelSal = new Double(SinglS.getText().trim()).doubleValue(); MyButton.this.setText(HouseAr + "\n" + HouseNu); indexof().setHouseNu(A+N); Write(); this.hide(); } if (arg0.getSource() == Cansal) { this.hide(); } } } //留客信息录入 class GetCustomerInfo extends JFrame implements ActionListener { private static final long serialVersionUID = 4824740927444846504L; private JPanel p, p1, p2, p3,p4,p5,p6; private JLabel Name, SumCash,SumDays,Sex,TelePhone; private JTextField HouseA, HouseN,SumD,telePhone; private JButton Entrue, Cansal; private JLabel sex; private ButtonGroup bg; private JRadioButton man,waman; private String sexStr; public GetCustomerInfo() { super.setTitle("录入顾客信息"); p = new JPanel(); p1 = new JPanel(); p2 = new JPanel(); p3 = new JPanel(); p4 = new JPanel(); p5 = new JPanel(); p6 = new JPanel(); Name = new JLabel("旅客姓名:"); sex = new JLabel("性别: "); TelePhone = new JLabel("联系方式:"); SumCash = new JLabel("旅客押金:"); SumDays = new JLabel("留住天数:"); HouseA = new JTextField(10); telePhone = new JTextField(10); HouseN = new JTextField(10); SumD = new JTextField(10); Entrue = new JButton("确定"); Cansal = new JButton("取消"); bg = new ButtonGroup(); man = new JRadioButton("男"); waman = new JRadioButton("女"); bg.add(man); bg.add(waman); p6.add(sex); p6.add(man); p6.add(waman); Entrue.addActionListener(this); Cansal.addActionListener(this); man.addActionListener(this); waman.addActionListener(this); p.setLayout(new GridLayout(6, 1)); p1.add(Name); p1.add(HouseA); p2.add(SumCash); p2.add(HouseN); p3.add(Entrue); p3.add(Cansal); p4.add(SumDays); p4.add(SumD); p5.add(TelePhone); p5.add(telePhone); p.add(p1); p.add(p5); p.add(p6); p.add(p2); p.add(p4); p.add(p3); this.add(p); this.setLocation(450, 300); this.setVisible(true); this.pack(); } @SuppressWarnings("deprecation") public void actionPerformed(ActionEvent arg0) { if(arg0.getSource() == man) { sexStr = "男"; } if(arg0.getSource() == waman) { sexStr = "女"; } if (arg0.getSource() == Entrue) { customer = new Customer(HouseA.getText(),sexStr,telePhone.getText(), new Integer(SumD.getText().trim()).intValue(), new Integer(HouseN.getText().trim()).intValue()); Housecolor = Color.blue; MyButton.this.setBackground(Housecolor); indexof().setHouseColor(Housecolor); if(new Double(HouseN.getText().trim()).doubleValue() < SingelSal) { JOptionPane.showMessageDialog(null, "押金不足!"); } else { Write(); this.hide(); } } if (arg0.getSource() == Cansal) { this.hide(); } } } public static void main(String[] s) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); MyButton mb = new MyButton(); panel.add(mb); frame.add(panel); frame.setSize(600, 600); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

62,630

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧