java - 如何设置gridlayout jpanel的大小

标签 java swing layout grid-layout

我正在尝试设置 gridlayout jpanel 的大小。这是代码:

JFrame myFrame = new JFrame();    
    myFrame.setLayout(new FlowLayout());  
    myFrame.setLocation(400, 100);
    myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    JLabel jlMins = new JLabel("Number of minutes for tutoring session (should be a positive decimal number):  0.0");
    JLabel jlEarnings = new JLabel("Earnings in dollars and cents received (should be positive decimal number):  0.0");
    jtfMins = new JTextField(20); 
    jtfEarnings = new JTextField(20);
    JPanel jpMins = new JPanel(new BorderLayout());
    JPanel jpEarnings = new JPanel(new BorderLayout());
    jpMins.setPreferredSize(new Dimension(300,50));
    jpEarnings.setPreferredSize(new Dimension(300,50));
    jpMins.add(jlMins,BorderLayout.NORTH);
    jpMins.add(jtfMins,BorderLayout.CENTER);
    jpEarnings.add(jlEarnings,BorderLayout.NORTH);
    jpEarnings.add(jtfEarnings,BorderLayout.CENTER);
    JButton jbQuit = new JButton("Quit");
    JButton jbEnter = new JButton("Enter");
    JButton jbReport = new JButton("Run Report");
    jbQuit.setActionCommand("quit");
    jbEnter.setActionCommand("enter");
    jbReport.setActionCommand("report");
    jbQuit.addActionListener(this);
    jbEnter.addActionListener(this);
    jbReport.addActionListener(this);
    JPanel jpButtons = new JPanel(new GridLayout(4,1,0,20)); 
    jpButtons.setSize(new Dimension(50,150));
    jpButtons.add(jbEnter);  
    jpButtons.add(jbReport);
    jpButtons.add(jbQuit); 
    JPanel jpNorth = new JPanel(new BorderLayout());
    jpNorth.add(jpMins,BorderLayout.NORTH); 
    jpNorth.add(jpEarnings,BorderLayout.CENTER);
    jpNorth.add(jpButtons,BorderLayout.SOUTH);
    jtaReports = new JTextArea();
    jtaReports.setColumns(40);
    jtaReports.setRows(10);
    jtaReports.setLineWrap(true);
    JScrollPane jspReports = new JScrollPane(jtaReports);
    jspReports.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    JPanel jpSouth = new JPanel();
    jpSouth.setPreferredSize(new Dimension(350,200)); 
    jpSouth.add(jspReports); 
    JPanel jpMain = new JPanel(new BorderLayout()); 
    jpMain.add(jpNorth,BorderLayout.NORTH);
    jpMain.add(jpSouth,BorderLayout.SOUTH);
    jpMain.setPreferredSize(new Dimension(500,500));
    myFrame.setContentPane(jpMain);  
    myFrame.pack();
    myFrame.setVisible(true);  

面板名称是 jpButtons。以上代码中我主要讲的是这一段:

   JButton jbQuit = new JButton("Quit");
JButton jbEnter = new JButton("Enter");
JButton jbReport = new JButton("Run Report");
jbQuit.setActionCommand("quit");
jbEnter.setActionCommand("enter");
jbReport.setActionCommand("report");
jbQuit.addActionListener(this);
jbEnter.addActionListener(this);
jbReport.addActionListener(this);
JPanel jpButtons = new JPanel(new GridLayout(4,1,0,20)); 
jpButtons.setSize(new Dimension(50,150));
jpButtons.add(jbEnter);  
jpButtons.add(jbReport);
jpButtons.add(jbQuit); 

setSize 和 setPreferredSize 究竟是如何工作的,或者如何让它们在 jpanel、组件等上正常工作。

最佳答案

缩放和定位由布局管理器处理;让它发挥作用。

关于java - 如何设置gridlayout jpanel的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11654983/

相关文章:

java - 循环和 JOptionpane

java - 我可以使用 FileInputStream 而无需在开始时为其指定路径吗

java - Spring MVC + Thymeleaf : adding variable to all templates' context

java - 如何在 android studio 中将设定的字符串转换为语音?

java - NetBeans 的 HintsController 和 EventQueue

java - JPanel 未显示在另一个 JPanel 内

css - 如何在 Windows 8 Store 应用程序的不同设备上使用屏幕区域的所有高度设置自动列数?

html - 导航边框高度不一样

html - 隐藏的选项卡未显示正确的布局

java - 将文件从临时目录复制到两个不同的目录失败