(内有恶犬!!求救ing)java GUI位置问题,并关于JPanel和JButton的位置确定问题..
我建了一个Jpanel(500*400),和一个JLabel,两个JButton;把JLabel和两个JButton放入JPanel中;我想实现:1)JLabel在JPa...
我建了一个Jpanel(500*400),和一个JLabel,两个JButton;
把JLabel和两个JButton放入JPanel中;
我想实现:1)JLabel在JPanel中靠左,两个JButton并列放置且在JPanel中靠右
2)JLabel和两个JButton离JPanel的上边缘为距离50;
要求备注:设定位置距离不能用setBounds(),因为这会影响跨平台!! 展开
把JLabel和两个JButton放入JPanel中;
我想实现:1)JLabel在JPanel中靠左,两个JButton并列放置且在JPanel中靠右
2)JLabel和两个JButton离JPanel的上边缘为距离50;
要求备注:设定位置距离不能用setBounds(),因为这会影响跨平台!! 展开
2个回答
展开全部
年轻人拖什么呀,手动敲才是王道,等真正开发了再托
import javax.swing.*;
import java.awt.*;
public class Test extends JPanel {
private JLabel label1 = new JLabel("Destiny");
private JButton b1 = new JButton("SB");
private JButton b2 = new JButton("BS");
public Test(){
setLayout(new GridBagLayout());
GridBagConstraints con = new GridBagConstraints();
con.insets = new Insets(5,5,5,5);
con.gridx = 0;
con.gridy = 0;
add(label1, con);
con.gridx++;
con.weightx = 1.0;
con.fill = GridBagConstraints.HORIZONTAL;
add(Box.createHorizontalStrut(50), con);
con.gridx++;
con.weightx = 0;
con.fill = GridBagConstraints.NONE;
add(b1, con);
con.gridx++;
add(b2, con);
}
public static void main(String[] args){
JFrame f = new JFrame();
f.add(new Test());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
public class Test extends JPanel {
private JLabel label1 = new JLabel("Destiny");
private JButton b1 = new JButton("SB");
private JButton b2 = new JButton("BS");
public Test(){
setLayout(new GridBagLayout());
GridBagConstraints con = new GridBagConstraints();
con.insets = new Insets(5,5,5,5);
con.gridx = 0;
con.gridy = 0;
add(label1, con);
con.gridx++;
con.weightx = 1.0;
con.fill = GridBagConstraints.HORIZONTAL;
add(Box.createHorizontalStrut(50), con);
con.gridx++;
con.weightx = 0;
con.fill = GridBagConstraints.NONE;
add(b1, con);
con.gridx++;
add(b2, con);
}
public static void main(String[] args){
JFrame f = new JFrame();
f.add(new Test());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询