我试图将单选按钮设置为背景,以便用户选择。
这是代码..
public class FirstWindow extends JFrame {
private JTextField search;
private JRadioButton author,title,both;
private ButtonGroup grp;
public FirstWindow() {
super("My App");
setLayout(new BorderLayout());
JLabel backGround = new JLabel(new ImageIcon("C:\\Users\\Kareem Abdo\\Desktop\\3.Jpg"));
backGround.setLayout(null);
add(backGround);
search = new JTextField("Search...");
search.setFont(new Font("Arial",Font.PLAIN,16));
search.setSize(150, 30);
search.setLocation(20, 20);
backGround.add(search);
author = new JRadioButton("Author",true);
author.setLocation(20, 25);
backGround.add(author);
title = new JRadioButton("Title",false);
title.setLocation(25, 25);
backGround.add(title);
both = new JRadioButton("Both",false);
both.setLocation(250, 250);
backGround.add(both);
grp = new ButtonGroup();
grp.add(author);
grp.add(title);
grp.add(both);
但是单选按钮没有出现在屏幕上!
答案 0 :(得分:3)
JLabel (JLabel backGround = new JLabel
)尚未实施任何LayoutManager
,then you have to set the proper one,否则添加到JComponent
的任何JLabel
都不可见
或许更好的方法可以从Image (BufferedImage)
painted in paintComponent
to the JPanel
开始(在API中预先实现FlowLayout
)