Java 如何设置gridlayout jpanel的大小
我正在尝试设置gridlayout jpanel的大小。代码如下:Java 如何设置gridlayout jpanel的大小,java,swing,layout,grid-layout,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("N
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、组件等上正常工作。缩放和定位由布局管理器处理;让它完成它的工作。缩放和定位由布局管理器处理;让它完成它的工作。另一种选择是扩展
JPanel
并覆盖它的方法:-)@GagandeepBali,我认为解决方法不适用于特定的布局管理器。最好的解决方法是将组件嵌套在直接添加到布局管理器的JPanel
实例中。或者更好的方法是,使用合适的布局管理器。我不好,从来没有看过讨论中的GridLayout:(另一种选择是扩展JPanel
并覆盖它的方法:-)@GagandeepBali,我认为解决方法不适用于特定的布局管理器。最好的解决方法是将组件嵌套在直接添加到布局管理器的JPanel
实例中。或者更好的方法是,使用一个合适的布局管理器。我不好,从来没有看过这个问题中的GridLayout:(老兄,你的代码受到了老兄的严重影响,你的代码受到了老兄的严重影响