import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
public class LayoutDemo extends JFrame {
LayoutDemo() {
super("LayoutDemo");
super.setSize(400, 200);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createGUI();
super.setVisible(true);
}
private void createGUI() {
// set the layout of this frame
super.setLayout(new BorderLayout());
// create a panel to put the button on
final JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
// create a text area to put in the center
final JTextArea textArea = new JTextArea();
// create the search button
final JButton searchButton = new JButton("search");
// add a listener to the button that add some text to the text area
searchButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
textArea.setText(textArea.getText() + "pressed search on " + (new Date()) + "\n");
}
});
// add the button to the bottom panel
bottomPanel.add(searchButton);
// wrap a scroll-pane around the text area and place it on the center of this frame
super.add(new JScrollPane(textArea), BorderLayout.CENTER);
// put the bottom panel (containing the button) on the 'south' of this frame
super.add(bottomPanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new LayoutDemo();
}
});
}
}
final JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 10));
请注意,按钮和文本区域之间的空间也略有增加!
I see you're using a GUI designer. I highly recommend building your GUI "by hand" instead in which case your code is IMO much clearer (I'm not saying all GUI designers produce bad code, but it is almost always harder to read, and editing it will be hard without using the exact same GUI designer). Once you're comfortable with GUI designing by hand, then try a GUI designer and see what makes you more comfortable.
In your case, you might create a BorderLayout, and in the "south" of your panel/frame you can place a panel with a FlowLayout aligning it's components to the left. Then add your button to the panel with the FlowLayout.
A little demo:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
public class LayoutDemo extends JFrame {
LayoutDemo() {
super("LayoutDemo");
super.setSize(400, 200);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createGUI();
super.setVisible(true);
}
private void createGUI() {
// set the layout of this frame
super.setLayout(new BorderLayout());
// create a panel to put the button on
final JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
// create a text area to put in the center
final JTextArea textArea = new JTextArea();
// create the search button
final JButton searchButton = new JButton("search");
// add a listener to the button that add some text to the text area
searchButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
textArea.setText(textArea.getText() + "pressed search on " + (new Date()) + "\n");
}
});
// add the button to the bottom panel
bottomPanel.add(searchButton);
// wrap a scroll-pane around the text area and place it on the center of this frame
super.add(new JScrollPane(textArea), BorderLayout.CENTER);
// put the bottom panel (containing the button) on the 'south' of this frame
super.add(bottomPanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new LayoutDemo();
}
});
}
}
learn fest swing test and miglayout! fest swing test enables you to run your gui screnario. and Miglayout,it is my opinion, also is easy to use layout lib.
If you are not trying to learn the ins & outs of Java Swing and aren't trying to create some fancy GUI then a GUI designer like the one you are using should be fine.
What you are not able to do seems to be a limitation of your particular IDE though, and therefore it might be helpful to give Netbeans a try. You can always take the generated GUI code (ugly as it may be) and then plug it back into your project in your other IDE.
发布评论
评论(3)
我发现您正在使用 GUI 设计器。我强烈建议“手动”构建 GUI,在这种情况下,我的代码会更清晰(我并不是说所有 GUI 设计者都会生成糟糕的代码,但它几乎总是难以阅读,并且在不使用的情况下编辑它会很困难)完全相同的 GUI 设计器)。一旦您对手动 GUI 设计感到满意,就可以尝试 GUI 设计器,看看什么能让您更舒服。
请参阅: http://java.sun.com/docs/ books/tutorial/uiswing/layout/using.html
在您的情况下,您可以创建一个 BorderLayout,并且在面板/框架的“南面”,您可以放置一个带有 FlowLayout 将其组件向左对齐。然后使用 FlowLayout 将按钮添加到面板。
一个小演示:
产生:
alt 文本 http://img689.imageshack.us/img689/ 5874/guiq.png
编辑
要将按钮向上移动一点,请使用构造函数
new FlowLayout(FlowLayout.LEFT, int hgap, int vgap)
其中,hgap 是左右组件之间的间隙(以像素为单位),vgap 是上下组件之间的间隙(以像素为单位)。
尝试:
请注意,按钮和文本区域之间的空间也略有增加!
I see you're using a GUI designer. I highly recommend building your GUI "by hand" instead in which case your code is IMO much clearer (I'm not saying all GUI designers produce bad code, but it is almost always harder to read, and editing it will be hard without using the exact same GUI designer). Once you're comfortable with GUI designing by hand, then try a GUI designer and see what makes you more comfortable.
See: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
In your case, you might create a BorderLayout, and in the "south" of your panel/frame you can place a panel with a FlowLayout aligning it's components to the left. Then add your button to the panel with the FlowLayout.
A little demo:
produces:
alt text http://img689.imageshack.us/img689/5874/guiq.png
EDIT
And to move the button a bit more up, use the constructor
new FlowLayout(FlowLayout.LEFT, int hgap, int vgap)
where
hgap
is the gap (in pixels) between the left and right components andvgap
is the gap (in pixels) between the upper and lower components.Try:
Note that the space between the button and text area also increases slightly!
学习
fest swing test
和 miglayout! fest swing 测试使您能够运行 GUI 场景。和
Miglayout
,我认为,也是易于使用的布局库。learn
fest swing test
and miglayout! fest swing test enables you to run your gui screnario.and
Miglayout
,it is my opinion, also is easy to use layout lib.如果您不想学习ins &如果您脱离了 Java Swing,并且不想创建一些花哨的 GUI,那么像您正在使用的 GUI 设计器应该没问题。
不过,您无法执行的操作似乎是您的特定 IDE 的限制,因此提供 Netbeans 尝试一下。您始终可以获取生成的 GUI 代码(尽管可能很丑),然后将其插回到其他 IDE 中的项目中。
If you are not trying to learn the ins & outs of Java Swing and aren't trying to create some fancy GUI then a GUI designer like the one you are using should be fine.
What you are not able to do seems to be a limitation of your particular IDE though, and therefore it might be helpful to give Netbeans a try. You can always take the generated GUI code (ugly as it may be) and then plug it back into your project in your other IDE.