以下java程序,如何实现按钮垂直排序,且按钮大小不变。其他的实现不变。
importjavax.swing.*;importjava.awt.*;publicclassDashboard{privatestaticfinallongseria...
import javax.swing.*;
import java.awt.*;
public class Dashboard{
private static final long serialVersionUID = 1L;
JMenu m,m1;
JMenuBar mb;
public Dashboard(){
JFrame jf = new JFrame("Dashboard");
jf.setSize(400,300);
jf.setLocationRelativeTo(null);//窗口居中
jf.setBackground(Color.white);
m = new JMenu("File");
m1 = new JMenu("Help");
mb = new JMenuBar();
mb.add(m);
mb.add(m1);
JPanel Panel_left = new JPanel();
Panel_left.setBackground(Color.white);
Panel_left.setBorder(BorderFactory.createMatteBorder(5,5,5,10,Color.lightGray));
JPanel Panel_right = new JPanel();
Panel_right.setBackground(Color.white);
Panel_right.setBorder(BorderFactory.createMatteBorder(5,5,5,5,Color.lightGray));
JButton b_movementforecast = new JButton("Movement Forecast");
b_movementforecast.setPreferredSize(new Dimension(150, 40));
JButton b_yarddensity = new JButton("Yard Density");
b_yarddensity.setLocation(6, 60);
b_yarddensity.setPreferredSize(new Dimension(150, 40));
Panel_left.add(b_movementforecast);
Panel_left.add(b_yarddensity);
jf.setJMenuBar(mb);
jf.getContentPane().add(Panel_left,"West") ;
jf.add(Panel_right);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Dashboard();
}
} 展开
import java.awt.*;
public class Dashboard{
private static final long serialVersionUID = 1L;
JMenu m,m1;
JMenuBar mb;
public Dashboard(){
JFrame jf = new JFrame("Dashboard");
jf.setSize(400,300);
jf.setLocationRelativeTo(null);//窗口居中
jf.setBackground(Color.white);
m = new JMenu("File");
m1 = new JMenu("Help");
mb = new JMenuBar();
mb.add(m);
mb.add(m1);
JPanel Panel_left = new JPanel();
Panel_left.setBackground(Color.white);
Panel_left.setBorder(BorderFactory.createMatteBorder(5,5,5,10,Color.lightGray));
JPanel Panel_right = new JPanel();
Panel_right.setBackground(Color.white);
Panel_right.setBorder(BorderFactory.createMatteBorder(5,5,5,5,Color.lightGray));
JButton b_movementforecast = new JButton("Movement Forecast");
b_movementforecast.setPreferredSize(new Dimension(150, 40));
JButton b_yarddensity = new JButton("Yard Density");
b_yarddensity.setLocation(6, 60);
b_yarddensity.setPreferredSize(new Dimension(150, 40));
Panel_left.add(b_movementforecast);
Panel_left.add(b_yarddensity);
jf.setJMenuBar(mb);
jf.getContentPane().add(Panel_left,"West") ;
jf.add(Panel_right);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Dashboard();
}
} 展开
2个回答
展开全部
进行容器的嵌套就能解决:
import javax.swing.*;
import java.awt.*;
public class Dashboard{
private static final long serialVersionUID = 1L;
JMenu m,m1;
JMenuBar mb;
public Dashboard(){
JFrame jf = new JFrame("Dashboard");
jf.setSize(700,300);
jf.setLocationRelativeTo(null);//窗口居中
jf.setBackground(Color.white);
m = new JMenu("File");
m1 = new JMenu("Help");
mb = new JMenuBar();
mb.add(m);
mb.add(m1);
JPanel Panel1 = new JPanel();
Panel1.setLayout(new GridLayout(5,3));
JLabel l1=new JLabel("");
JLabel l2=new JLabel("");
JLabel l3=new JLabel("");
JLabel l4=new JLabel("");
JLabel l5=new JLabel("");
JLabel l6=new JLabel("");
JLabel l7=new JLabel("");
JLabel l8=new JLabel("");
JLabel l9=new JLabel("");
JLabel l10=new JLabel("");
JLabel l11=new JLabel("");
JLabel l12=new JLabel("");
JLabel l13=new JLabel("");
JPanel Panel_left = new JPanel();
Panel_left.setBackground(Color.white);
Panel_left.setBorder(BorderFactory.createMatteBorder(5,5,5,10,Color.lightGray));
JPanel Panel_right = new JPanel();
Panel_right.setBackground(Color.white);
Panel_right.setBorder(BorderFactory.createMatteBorder(5,5,5,5,Color.lightGray));
JButton b_movementforecast = new JButton("Movement Forecast");
b_movementforecast.setPreferredSize(new Dimension(150, 40));
JButton b_yarddensity = new JButton("Yard Density");
b_yarddensity.setLocation(6, 60);
b_yarddensity.setPreferredSize(new Dimension(150, 40));
Panel1.add(l1);
Panel1.add(l2);
Panel1.add(l3);
Panel1.add(l4);
Panel1.add(b_movementforecast);
Panel1.add(l5);
Panel1.add(l6);
Panel1.add(l7);
Panel1.add(l8);
Panel1.add(l9);
Panel1.add(b_yarddensity);
Panel1.add(l10);
Panel1.add(l11);
Panel1.add(l12);
Panel1.add(l13);
Panel_left.add(Panel1);
jf.setJMenuBar(mb);
jf.getContentPane().add(Panel_left,"West") ;
jf.add(Panel_right);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Dashboard();
}
}
import javax.swing.*;
import java.awt.*;
public class Dashboard{
private static final long serialVersionUID = 1L;
JMenu m,m1;
JMenuBar mb;
public Dashboard(){
JFrame jf = new JFrame("Dashboard");
jf.setSize(700,300);
jf.setLocationRelativeTo(null);//窗口居中
jf.setBackground(Color.white);
m = new JMenu("File");
m1 = new JMenu("Help");
mb = new JMenuBar();
mb.add(m);
mb.add(m1);
JPanel Panel1 = new JPanel();
Panel1.setLayout(new GridLayout(5,3));
JLabel l1=new JLabel("");
JLabel l2=new JLabel("");
JLabel l3=new JLabel("");
JLabel l4=new JLabel("");
JLabel l5=new JLabel("");
JLabel l6=new JLabel("");
JLabel l7=new JLabel("");
JLabel l8=new JLabel("");
JLabel l9=new JLabel("");
JLabel l10=new JLabel("");
JLabel l11=new JLabel("");
JLabel l12=new JLabel("");
JLabel l13=new JLabel("");
JPanel Panel_left = new JPanel();
Panel_left.setBackground(Color.white);
Panel_left.setBorder(BorderFactory.createMatteBorder(5,5,5,10,Color.lightGray));
JPanel Panel_right = new JPanel();
Panel_right.setBackground(Color.white);
Panel_right.setBorder(BorderFactory.createMatteBorder(5,5,5,5,Color.lightGray));
JButton b_movementforecast = new JButton("Movement Forecast");
b_movementforecast.setPreferredSize(new Dimension(150, 40));
JButton b_yarddensity = new JButton("Yard Density");
b_yarddensity.setLocation(6, 60);
b_yarddensity.setPreferredSize(new Dimension(150, 40));
Panel1.add(l1);
Panel1.add(l2);
Panel1.add(l3);
Panel1.add(l4);
Panel1.add(b_movementforecast);
Panel1.add(l5);
Panel1.add(l6);
Panel1.add(l7);
Panel1.add(l8);
Panel1.add(l9);
Panel1.add(b_yarddensity);
Panel1.add(l10);
Panel1.add(l11);
Panel1.add(l12);
Panel1.add(l13);
Panel_left.add(Panel1);
jf.setJMenuBar(mb);
jf.getContentPane().add(Panel_left,"West") ;
jf.add(Panel_right);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Dashboard();
}
}
追问
简而言之就是加层嵌套吧,JAVA的GUI方面确实没有做的C++的MFC好。谢谢了。
展开全部
Panel_left.setLayout(new GridLayout(2, 1));//添加这一句
Panel_left.add(b_movementforecast);
Panel_left.add(b_yarddensity);
frmae得size也要调整下比较好
jf.setSize(300, 200);
或者最后面加上jf.pack();
Panel_left.add(b_movementforecast);
Panel_left.add(b_yarddensity);
frmae得size也要调整下比较好
jf.setSize(300, 200);
或者最后面加上jf.pack();
追问
使用过你的方法,按钮的大小不变,这个条件不符合了!
追答
大小需要慢慢调节,这个自己弄吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询