私はJFrame mainFrameを持つクラスBaseWindowと、mainFrameで動作するメソッドの束を持っています。私のアプリの実際のウィンドウは、BaseWindowを拡張することによって作られています。Eclipse WindowBuilderを継承したJFrameを解析する方法を教えてください。
BaseWindowを継承するクラスはmainFrameを継承しているため、コードを解析するときにウィンドウビルダが「参照」しないようにします。ウィンドウビルダのデザインタブに表示されるのは空のウィンドウですが、コードを実行するとすべて正常に動作します。
このアプローチをウィンドウビルダで動作させるにはどうすればよいですか、または「トリック」ウィンドウビルダを使ってmainFrameを解析しますか?以下は、BaseWindowを拡張したクラスの例です:
package GUIApp;
//took out the imports to save space
class SampleWindowOne extends BaseWindow{
public JLabel txtSampleText;
public JButton btnMagicButton;
public SampleWindowOne() {
initialize();
}
public void run(){
mainFrame.setBounds(getCenteredBounds());
mainFrame.setVisible(true);
}
private void initialize() {
mainFrame = new JFrame();
mainFrame.setBounds(100,100,700,525);
mainFrame.setTitle("Sample Window One");
mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mainFrame.getContentPane().setLayout(null);
txtSampleText = new JLabel();
txtSampleText.setFont(new Font("Dialog", Font.PLAIN, 25));
txtSampleText.setHorizontalAlignment(SwingConstants.CENTER);
txtSampleText.setText("Sample Window");
txtSampleText.setBounds(12, 50, 674, 51);
mainFrame.getContentPane().add(txtSampleText);
JTextArea txtrLoremIpsumDolor = new JTextArea();
txtrLoremIpsumDolor.setEditable(false);
txtrLoremIpsumDolor.setFont(new Font("Dialog", Font.PLAIN, 16));
txtrLoremIpsumDolor.setForeground(Color.DARK_GRAY);
txtrLoremIpsumDolor.setWrapStyleWord(true);
txtrLoremIpsumDolor.setLineWrap(true);
txtrLoremIpsumDolor.setText("Lorem ipsum dolor sit amet...");
txtrLoremIpsumDolor.setBounds(57, 131, 609, 296);
mainFrame.getContentPane().add(txtrLoremIpsumDolor);
btnMagicButton = new JButton("End Program");
btnMagicButton.setEnabled(false);
btnMagicButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
requestTerminate();
}
});
btnMagicButton.setBounds(257, 451, 175, 25);
mainFrame.getContentPane().add(btnMagicButton);
}
}
'txtrLoremIpsumDolor.setBounds(57,131,609,296);' ?? –
注:私はウィンドウ・ビルダーを使用しませんが、Swingコーディングをたくさん行い、JTextAreaの自然な振る舞いを制限するため、上記の行は決して存在するべきではありません。 JTextAreasはJScrollPanes内に属し、このようにサイズを制限すると、テキスト領域が拡大されないようになります。つまり、スクロールバーは決して動作しません。基本的なライブラリをよりよく理解するまで、Windowsビルダーの使用を断念することをお勧めします。 –
良い情報、ホバークラフト。私はSwingの深い理解から恩恵を受けることは間違いありませんが、現時点ではWYSIWYGの設計機能を再び働かせる必要があります。私は今までに教えられてきたGUIデザインが非常に初歩的で、独占的にウィンドウビルダを使っているクラスのコードを書いていますので、私のグループメンバーと私は現在のプロジェクトでそれを使用できる必要があります。 – jgardner7289