私はJavaでGUIに取り組んでいて、オブジェクトの移動に行き詰まりました。
私が何をしようとしていたかを見るためにあなたたちのために私が短いデモを作ったこのユーチューブビデオを訪問してください。私はGUIのことを教えられたことがないので、GUIのことはとても初めてです。
リンクは次のとおりです:http ://www.youtube.com/watch?v = up1LV5r-NSg
私はJavaでGUIに取り組んでいて、オブジェクトの移動に行き詰まりました。
私が何をしようとしていたかを見るためにあなたたちのために私が短いデモを作ったこのユーチューブビデオを訪問してください。私はGUIのことを教えられたことがないので、GUIのことはとても初めてです。
リンクは次のとおりです:http ://www.youtube.com/watch?v = up1LV5r-NSg
GUIデザイナを使用しているようです。代わりに「手作業」でGUIを作成することを強くお勧めします。その場合、コードはIMOではるかに明確になります(すべてのGUIデザイナーが悪いコードを作成するとは限りませんが、ほとんどの場合、読みにくく、使用せずに編集するのは困難です。まったく同じGUIデザイナー)。手作業でのGUI設計に慣れたら、GUIデザイナーを試して、何がより快適になるかを確認してください。
参照: http: //java.sun.com/docs/books/tutorial/uiswing/layout/using.html
あなたの場合、BorderLayoutを作成し、パネル/フレームの「南」に、コンポーネントを左に揃えるFlowLayoutを使用してパネルを配置できます。次に、FlowLayoutを使用してボタンをパネルに追加します。
ちょっとしたデモ:
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();
}
});
}
}
生成:
代替テキストhttp://img689.imageshack.us/img689/5874/guiq.png
編集
ボタンをもう少し上に移動するには、コンストラクターを使用しますnew FlowLayout(FlowLayout.LEFT, int hgap, int vgap)
ここhgap
で、は左右のコンポーネント間vgap
のギャップ(ピクセル単位)であり、は上部コンポーネントと下部コンポーネント間のギャップ(ピクセル単位)です。
試す:
final JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 10));
ボタンとテキスト領域の間のスペースもわずかに増加することに注意してください!
学びfest swing test
、miglayout!フェストスイングテストを使用すると、GUIスクリーンを実行できます。そしてMiglayout
、それは私の意見ですが、レイアウトライブラリも使いやすいです。
Fest: http://fest.easytesting.org/swing/wiki/pmwiki.php
MigLayout: http://www.miglayout.com/
Java Swing の詳細を学習しようとしておらず、派手な GUI を作成しようとしていない場合は、使用しているような GUI デザイナーで問題ありません。
あなたができないことは、あなたの特定の IDE の制限のように思われるので、Netbeansを試してみるとよいでしょう。生成された GUI コード (見苦しいかもしれませんが) をいつでも取得して、別の IDE のプロジェクトにプラグインし直すことができます。