我目前正在努力为自己做一个小项目,以帮助我完成日常琐事。我试图在Swing中创建一个小表并开始添加组件。但是,我面临一些困难。
首先,让我强调一下我需要实现的目标。下面的照片:
根据下面的代码示例,我尝试创建一个主容器,我已将其大小设置为600 x 300,然后创建3个JPanels(左,右和UpperBothJPanel以包含其他两个)。
我已将GridLayout设置为UpperBothJPanel,以使两个(左右JPanels)具有相同大小的Table和GridBagLayout,因此我可以单独修改它们的宽度。 然后我在左侧添加了3个JLabel,在右侧添加了3个JLabel。 然后将右侧和左侧JPanel添加到UpperBothJPanel。
然而,我遇到了一些困难:
左右JLabel偏移且未完全对齐,但左右JLabel的代码相同。
import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Toolkit; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel;
public class Main扩展JFrame {
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();
}
});
}
}
提前谢谢你, 最好的问候