如何使用布局
获得相同的外观 package item;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author isslam
*/
public class MyFrameMain extends JFrame{
Equipment newq = new Equipment();
private final JLabel iLabel;
private final JLabel nLabel;
private final JTextField iJTextField;
private final JTextField nJTextField;
private final JTextField swTextField;
private final JTextField hwTextField;
private final JLabel jItemCounter;
private final JTextArea reSoulte;
private final JButton addButton;
private final JButton showButton;
private final JButton copyButton;
private final JButton exitButton;
public MyFrameMain(String title){
super(title);
setTitle(title);
setDefaultCloseOperation(MyFrameMain.EXIT_ON_CLOSE);
iJTextField = new JTextField();
nJTextField = new JTextField();
swTextField = new JTextField();
hwTextField = new JTextField();
nLabel = new JLabel("ID: ");
iLabel = new JLabel("Name: ");
jItemCounter = new JLabel("Number of current Item");
reSoulte = new JTextArea(15,20);
reSoulte.setEditable(false);
addButton = new JButton("Add an item into the Array");
showButton = new JButton("Show all items in the Array");
copyButton = new JButton("Copy Array into File");
exitButton = new JButton("Exite");
JRadioButton rButton1 = new JRadioButton("SW Version",false);
JRadioButton rButton2 = new JRadioButton("HW Type",false);
JRadioButton rButton3 = new JRadioButton("General",true);
ButtonGroup BGroup = new ButtonGroup();
BGroup.add(rButton1);
BGroup.add(rButton2);
BGroup.add(rButton3);
JPanel panel = new JPanel(new GridLayout(5,1));
panel.add(nLabel);
panel.add(iLabel);
panel.add(rButton1);
panel.add(rButton2);
panel.add(rButton3);
JPanel bpanel = new JPanel(new GridLayout(2,2));
bpanel.add(addButton);
bpanel.add(showButton);
bpanel.add(copyButton);
bpanel.add(exitButton);
JPanel jtfPanel = new JPanel(new GridLayout(5,1));
jtfPanel.add(iJTextField);
jtfPanel.add(nJTextField);
jtfPanel.add(swTextField);
jtfPanel.add(hwTextField);
jtfPanel.add(jItemCounter);
JPanel jtaPanel = new JPanel(new GridLayout(5,1));
jtaPanel.add(reSoulte);
Container pane = getContentPane();
pane.setLayout(null);
pane.add(panel);
pane.add(bpanel);
pane.add(jtfPanel);
pane.add(jtaPanel);
Insets insets = pane.getInsets();
setSize(500 + insets.left + insets.right,500 + insets.top + insets.bottom);
Dimension size = panel.getPreferredSize();
panel.setBounds(0 + insets.left, 18 + insets.top,size.width, size.height);
size = bpanel.getPreferredSize();
bpanel.setBounds(0 + insets.left, 409 + insets.top,115+size.width, size.height);
size = jtfPanel.getPreferredSize();
jtfPanel.setBounds(180 + insets.left, 25 + insets.top,170 +size.width, size.height);
size = jtaPanel.getPreferredSize();
jtaPanel.setBounds(0 + insets.left, 150 + insets.top,265 +size.width, size.height);
exitButton.addActionListener(new ButtonWatcher());
}
private class ButtonWatcher implements ActionListener{
public void actionPerformed(ActionEvent a){
System.exit(0);
}
}
}
答案 0 :(得分:1)
你能告诉我如何将绝对位置替换为borderlayout
我看到你的代码的方式:
BorderLayout有3个垂直区域可以与NORTH,CENTER和SOUTH一起使用,所以我建议你将“panel”和“jtfPanel”组合到另一个面板中,然后你将有3个面板添加到BorderLayout。类似的东西:
JPanel north = new JPanel( new BorderLayout() );
north.add(panel, BorderLayout.WEST);
north.add(jtfPanel, BorderLayout.CENTER);
Container contentPane = getContentPane();
contentPane.add(north, BorderLayout.NORTH);
contentPane.add(jtaPanel, BorderLayout.CENTER);
contentPane.add(bPanel, BorderLayout.SOUTH);
重点是您可以嵌套多个面板以达到您想要的效果。
答案 1 :(得分:1)
在这里查看一下。我添加了一个主要方法来运行它。我使用EmptyBorder
s来获得所需的间距。我还将面板组合在一起,使布局更容易使用。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.EmptyBorder;
/**
*
* @author isslam
*/
public class MyFrameMain extends JFrame {
// Equipment newq = new Equipment();
private final JLabel iLabel;
private final JLabel nLabel;
private final JTextField iJTextField;
private final JTextField nJTextField;
private final JTextField swTextField;
private final JTextField hwTextField;
private final JLabel jItemCounter;
private final JTextArea reSoulte;
private final JButton addButton;
private final JButton showButton;
private final JButton copyButton;
private final JButton exitButton;
public MyFrameMain(String title) {
//super(title);
setTitle(title);
setDefaultCloseOperation(MyFrameMain.EXIT_ON_CLOSE);
iJTextField = new JTextField();
nJTextField = new JTextField();
swTextField = new JTextField();
hwTextField = new JTextField();
nLabel = new JLabel("ID: ");
iLabel = new JLabel("Name: ");
jItemCounter = new JLabel("Number of current Item");
reSoulte = new JTextArea(15, 45);
reSoulte.setEditable(false);
addButton = new JButton("Add an item into the Array");
showButton = new JButton("Show all items in the Array");
copyButton = new JButton("Copy Array into File");
exitButton = new JButton("Exite");
JRadioButton rButton1 = new JRadioButton("SW Version", false);
JRadioButton rButton2 = new JRadioButton("HW Type", false);
JRadioButton rButton3 = new JRadioButton("General", true);
ButtonGroup BGroup = new ButtonGroup();
BGroup.add(rButton1);
BGroup.add(rButton2);
BGroup.add(rButton3);
JPanel panel = new JPanel(new GridLayout(5, 1));
panel.setBorder(new EmptyBorder(0, 0, 0, 75));
panel.add(nLabel);
panel.add(iLabel);
panel.add(rButton1);
panel.add(rButton2);
panel.add(rButton3);
JPanel bpanel = new JPanel(new GridLayout(2, 2));
bpanel.setBorder(new EmptyBorder(15, 0, 0, 0));
bpanel.add(addButton);
bpanel.add(showButton);
bpanel.add(copyButton);
bpanel.add(exitButton);
JPanel jtfPanel = new JPanel(new GridLayout(5, 1));
jtfPanel.add(iJTextField);
jtfPanel.add(nJTextField);
jtfPanel.add(swTextField);
jtfPanel.add(hwTextField);
jtfPanel.add(jItemCounter);
//JPanel jtaPanel = new JPanel(new GridLayout(5, 1));
//jtaPanel.add(reSoulte);
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.setBorder(new EmptyBorder(0, 0, 20, 0));
topPanel.add(panel, BorderLayout.WEST);
topPanel.add(jtfPanel, BorderLayout.CENTER);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.setBorder(new EmptyBorder(20, 10, 10, 10));
mainPanel.add(bpanel, BorderLayout.SOUTH);
mainPanel.add(reSoulte, BorderLayout.CENTER);
mainPanel.add(topPanel, BorderLayout.NORTH);
Container pane = getContentPane();
//pane.setLayout(null);
//pane.add(panel);
pane.add(mainPanel);
//pane.add(jtfPanel);
//Insets insets = pane.getInsets();
//setSize(500 + insets.left + insets.right, 500 + insets.top + insets.bottom);
//Dimension size = panel.getPreferredSize();
//panel.setBounds(0 + insets.left, 18 + insets.top, size.width, size.height);
//size = bpanel.getPreferredSize();
// bpanel.setBounds(0 + insets.left, 409 + insets.top, 115 + size.width, size.height);
// size = jtfPanel.getPreferredSize();
// jtfPanel.setBounds(180 + insets.left, 25 + insets.top, 170 + size.width, size.height);
//size = jtaPanel.getPreferredSize();
//jtaPanel.setBounds(0 + insets.left, 150 + insets.top, 265 + size.width, size.height);
exitButton.addActionListener(new ButtonWatcher());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
MyFrameMain frame = new MyFrameMain("Title");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setResizable(false);
//frame.setSize(500, 500);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
}
private class ButtonWatcher implements ActionListener {
public void actionPerformed(ActionEvent a) {
System.exit(0);
}
}
}