2017-03-13 17 views
0

私は "Core Java Volume I- Fundamentals 9th Edition- Horstmann、Cay S. & Cornell、Gary_2013"という本を読んでいて、まだJavaを学んでいますが、SwingとAWTがあります。本にある1つのタスクに関する質問。 タスクは、ユーザー名とパスワードを持つ3つのセクターのNorthernを持つフレームを構築することです。テキスト領域を中央に、南をボタンで囲みます。メインフレームは境界線レイアウトになっています。ユーザーがユーザー名とパスワードを入力し、「挿入」ボタンをクリックすると、ユーザー名とパスワードがテキスト領域に入力されます。ここでは、結果の画像です:私は間違っ enter image description here私のメインフレームにJavaスイングでは何もありません


:私はこれを実行しようとすると、 enter image description here
は、だから私はこのような小さなウィンドウを得ましたか。私はこれを理解できません。 これはJFrameの

import java.awt.*; 
import javax.swing.*; 
import java.lang.*; 

public class MainFrame extends JFrame { 
    public MainFrame() { 
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     int width = screenSize.width; 
     int height = screenSize.height; 
     setSize(width/2, height/2); 
     setLayout(new BorderLayout()); 

     JPanel northPanel = new JPanel(); 
     northPanel.setLayout(new GridLayout(2,2)); 
     northPanel.add(new JLabel("Username: ", JLabel.RIGHT)); 
     JTextField textField = new JTextField(); 
     northPanel.add(textField); 
     northPanel.add(new JLabel("Password: ", JLabel.RIGHT)); 
     JPasswordField passwordField = new JPasswordField(); 
     northPanel.add(passwordField); 

     add(northPanel, BorderLayout.NORTH); 


     JTextArea textArea = new JTextArea(8, 20); 
     JScrollPane centerPanel = new JScrollPane(textArea); 


     add(centerPanel, BorderLayout.CENTER); 

     JPanel southPanel = new JPanel(); 
     JButton insertButton = new JButton("Insert"); 
     southPanel.add(insertButton); 

     add(southPanel, BorderLayout.SOUTH);  
    } 
} 

を拡張するクラスであると私MainTestMethodが似ている:

import java.awt.*; 
import javax.swing.*; 

public class MainTestProgram { 
    public static void main(String[] args) { 
     JFrame mainFrame = new JFrame(); 
     mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
     mainFrame.setTitle("Krisko Beatz"); 
     mainFrame.setVisible(true); 
     mainFrame.setLocation(500, 100); 
    } 
} 
+1

JFrame mainFrame = new JFrame();をJFrame mainFrame = new MainFrame();に変更します – MadProgrammer

+1

入力ミス。 –

答えて

3

変更JFrame mainFrame = new JFrame();あなたが削除することができます

JFrame mainFrame = new MainFrame();に...

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    int width = screenSize.width; 
    int height = screenSize.height; 
    setSize(width/2, height/2); 

と代わりにに電話をかけてくださいコンストラクタの最後に、より信頼性の高いUIサイズが生成されます

+0

ありがとうございました – user7460099

+1

あなたは答えとして受け入れる –

関連する問題