当您在JLabel中有背景时,如何将bottom设置到特定的位置:下面的代码无法使jlabel停留在顶部,而按钮位于南部(底部)?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonsClass extends JFrame
implements ActionListener {
JButton b1 = new JButton("button1");
JButton b2 = new JButton("button2");
JButton b3 = new JButton("button3");
JButton b4 = new JButton("button4");
JLabel label = new JLabel("buttons:");
public static void main(String[] args) {
new ButtonsClass();
}
public Jukebox() {
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("image.png")));
setLayout(new FlowLayout());
setSize(500,150);
setTitle("Backgroundwithbuttons");
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
JPanel top = new JPanel();
top.add(label);
add("North", top);
JPanel bottom = new JPanel();
bottom.add(b1);
bottom.add(b2);
bottom.add(b3);
bottom.add(b4);
add("South", bottom);
setVisible(true);
}
}
发布于 2014-03-26 09:46:09
“我不能让jlabel停留在顶部,让按钮保持在南部(底部)”
BorderLayout
,然后立即将其设置为FlowLayout
。有了FlowLayout
,您的BorderLayout
定位就什么也做不了。
setLayout(新BorderLayout());setContentPane(新ImageIcon(“image.png”));setLayout(新FlowLayout());只要摆脱setLayout(new FlowLayout());
JLabel
的布局。Yout构造函数应该如下所示
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);ButtonClass() { JLabel背景=新的JLabel(新的ImageIcon("image.png"));background.setLayout(新的BorderLayout());setContentPane(背景);setTitle(“带有按钮的背景”);setTitle(“带有按钮的背景”);setTitle JPanel top =新的JPanel();top.add(标签);add(顶上的BorderLayout.NORTH);JPanel底部=新的JPanel();bottom.add(JPanel);JPanel();bottom.add(b3);bottom.add(b4);添加(底部,BorderLayout.SOUTH);//pack();setVisible(真);}add("North", top);
是一种不推荐的方法。相反,可以使用add(top, BorderLayout.NORTH)
,而对add(bottom, BorderLayout.SOUTH)
使用相同的main
中使用SwingUtilities.invokeLater...
包装代码来做到这一点。
公共静态void (String[] args) { SwingUtilities.invokeLater(new ()){ public void (){新ButtonClass();};}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonClass extends JFrame
implements ActionListener {
JButton b1 = new JButton("button1");
JButton b2 = new JButton("button2");
JButton b3 = new JButton("button3");
JButton b4 = new JButton("button4");
JLabel label = new JLabel("buttons:");
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ButtonClass();
}
});
}
public ButtonClass() {
label.setForeground(Color.WHITE);
JLabel background = new JLabel(new ImageIcon(getClass().getResource("/resources/space.png")));
background.setLayout(new BorderLayout());
setContentPane(background);
setTitle("Background with buttons");
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
JPanel top = new JPanel();
top.setOpaque(false);
top.add(label);
add(top, BorderLayout.NORTH);
JPanel bottom = new JPanel();
bottom.setOpaque(false);
bottom.add(b1);
bottom.add(b2);
bottom.add(b3);
bottom.add(b4);
add(bottom, BorderLayout.SOUTH);
setSize(400, 300);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {}
}
发布于 2014-03-26 09:45:55
https://stackoverflow.com/questions/22668846
复制相似问题