Java 使用不同布局嵌套JPanel
我目前正试图为自己的工作做一个小项目,以帮助我完成日常琐事。我试图在Swing中创建一个小表,并开始添加组件。然而,我面临着一些困难 首先,让我强调一下我需要实现的目标。下图: 根据下面的代码示例,我尝试创建一个主容器,我将其大小设置为600 x 300,然后创建了3个JPanel(左、右和上面板以包含其他两个) 我已经将GridLayout设置为UpperBothJPanel,使其具有相同大小的表,并将GridBagLayout设置为两个(左侧和右侧JPanel),以便可以分别修改其宽度。 然后我在左侧添加了3个jlabel,在右侧添加了3个jlabel。 然后将右侧和左侧JPanel添加到上面板 然而,我遇到了一些困难:Java 使用不同布局嵌套JPanel,java,swing,jlabel,gridbaglayout,Java,Swing,Jlabel,Gridbaglayout,我目前正试图为自己的工作做一个小项目,以帮助我完成日常琐事。我试图在Swing中创建一个小表,并开始添加组件。然而,我面临着一些困难 首先,让我强调一下我需要实现的目标。下图: 根据下面的代码示例,我尝试创建一个主容器,我将其大小设置为600 x 300,然后创建了3个JPanel(左、右和上面板以包含其他两个) 我已经将GridLayout设置为UpperBothJPanel,使其具有相同大小的表,并将GridBagLayout设置为两个(左侧和右侧JPanel),以便可以分别修改其宽度。
public String frameTitle, projectNameString, countryNameString, projectDetailString, requestDateString;
private int offerRevisionVal;
public JFrame frame;
public Container mainContainer;
private Toolkit toolkit;
private JPanel upperBothPanels, upperLeftPanel, upperRightPanel, middlePanel, lowerPanel; // Main Panels where I added my smaller Objects
private GridBagLayout upperLeftGridLayout, upperRightGridLayout;
private GridLayout upperBothGridLayout;
// UPPER OBJECTS
private JLabel projectLabel, countryLabel, projectDetailLabel, offerLabel, requestDateLabel, emptyLabel;
public Main(){
frameTitle = "CRANES QUOTATIONS";
// MAIN INSTANCE VARIABLES DEFINITION
projectNameString = "Project Title"; // TO ADD LATER
countryNameString = "Brazil"; // TO ADD LATER
projectDetailString = "32 Cranes biTravi"; // TO ADD LATER
offerRevisionVal = 01; // TO ADD LATER
requestDateString = "20-April-2017"; // tTO ADD LATER
setTitle(frameTitle); // Added the main title of my App
mainContainer = getContentPane(); // set up a Container to contain all my objects
mainContainer.setBackground(Color.GREEN); // set the color of my ContentPane
// UPER PANEL DEFINITION
upperLeftPanel = new JPanel();
//upperLeftPanel.setBorder(BorderFactory.createDashedBorder(Color.red, 5, 5));
// Definition place of my upper Grid Bag Layouts. One to hold them all.
upperBothGridLayout = new GridLayout(2,1);
upperLeftGridLayout = new GridBagLayout();
upperLeftPanel.setBackground(Color.GREEN);
upperLeftPanel.setLayout(upperLeftGridLayout);
// PLACE LEFT GRID
GridBagConstraints c = new GridBagConstraints();
projectLabel = new JLabel(projectNameString);
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=0;
upperLeftPanel.add(projectLabel, c);
countryLabel = new JLabel(countryNameString);
c.weighty=0.5;
c.fill = GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=1;
upperLeftPanel.add(countryLabel, c);
projectDetailLabel = new JLabel(projectDetailString);
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 2;
upperLeftPanel.add(projectDetailLabel, c);
// PLACE RIGHT GRID
upperRightPanel = new JPanel();
upperRightPanel.setBackground(Color.WHITE);
upperRightGridLayout = new GridBagLayout();
upperRightPanel.setLayout(upperRightGridLayout);
//upperRightPanel.setBorder(BorderFactory.createDashedBorder(Color.BLUE, 5, 5));
GridBagConstraints d = new GridBagConstraints();
offerLabel = new JLabel(String.valueOf(offerRevisionVal));
d.weighty = 0.5;
d.fill = GridBagConstraints.BOTH;
d.gridx=0;
d.gridy=0;
upperRightPanel.add(offerLabel, d);
requestDateLabel = new JLabel(requestDateString);
d.weighty=0.5;
d.fill = GridBagConstraints.BOTH;
d.gridx=0;
d.gridy=1;
upperRightPanel.add(requestDateLabel, d);
emptyLabel = new JLabel();
d.weighty = 0.5;
d.fill = GridBagConstraints.BOTH;
d.gridx = 0;
d.gridy = 2;
upperRightPanel.add(emptyLabel, d);
upperBothPanels = new JPanel(new GridLayout(1,2));
upperBothPanels.setSize(150,200);
upperBothPanels.add(upperLeftPanel);
upperBothPanels.add(upperRightPanel);
// =========================================================================== ADD THE PANELS
mainContainer.add(upperBothPanels);
// ===========================================================================
setSize(600, 300); // set the height and width of my window
centerToScreen ();
setVisible(true); // set visibility
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // sets up what it does when I close the App.
}
// Method to center to screen
public void centerToScreen (){
toolkit = getToolkit();
Dimension size = toolkit.getScreenSize(); // gets the screen size
setLocation(size.width / 2 - getWidth() / 2, size.height / 2 - getHeight() / 2); // sets the location
}
}import javax.swing.SwingUtilities;
public class CranesApp {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
new Main();
}
});
}
}
提前谢谢大家,,
致以最诚挚的问候如何创建和使用JTable?如果我添加了一个JTable以包含我的3行2列(如上所述),那么我是否可以在一个新JTable的下面添加10行4列,并按照之前的JTable设置所有最大大小?