2016-04-08 9 views
-1

私は現在6つの異なるGUIを作成する必要がある仕事でプロジェクトを強化する作業を進めています。私はGUIの設計が非常に悪いと言い始めるべきです。私はグリッドバッグを学ぼうとしましたが、本当に難しいと感じました。JTextAreaプロジェクトの効率的なGUIデザイン

このGUIでは、私はBorderLayoutとフローレイアウトを使用していて、うまく動作します。私はGUIを作成して目的を達成することができますが、私の他のGUIがJTextFieldsとJLabelsをたくさん持ってしまうと、このメソッドがプロフェッショナルではなく、不要なコードがたくさんあるような気がします。意志)。

ここにあるパネルの量を減らすことができるレイアウトがあるかどうかを知りたいと思います。私はグリッドレイアウトを検討しましたが、すべてのセルが同じサイズであることを心配しています。誰かが、自分の経験から、このプロジェクトのための最良のレイアウトを教えてくれたら助けてくれるかもしれませんし、それをどう使うか私に教えてください。私はGridBagレイアウトのようなレイアウトチュートリアルをやってみましたが、実際に自分のプロジェクトに実装するのに問題があります。

メインクラス

​​

GUI

package gui; 

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 

import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class AllGUI 

{ 

    public void createAllGUI(){ 

     JFrame frame = new JFrame("All File Types Selection"); 
     JPanel mainPanel = new JPanel(new BorderLayout()); 
     JPanel panelOne = new JPanel(new BorderLayout()); 
     JPanel panelLine1 = new JPanel(new FlowLayout()); 
     JPanel panelLine2 = new JPanel(new FlowLayout()); 
     JPanel panelLine3 = new JPanel(new FlowLayout()); 
     JPanel panelLine4 = new JPanel(new FlowLayout()); 
     JPanel panelLine5 = new JPanel(new FlowLayout()); 

     JButton confirmButton = new JButton("Confirm"); 

     JLabel groupMessageIdTitle = new JLabel("Group Message Id:"); 
     JLabel isoDateTimeTitle = new JLabel("ISO Creation Date/Time:"); 
     JLabel notificationIdTitle = new JLabel("Notification Id:"); 
     JLabel notificationAcctIdTitle = new JLabel("Notification Account Id:"); 
     JLabel numberOfEntriesTitle = new JLabel("Number of Entries:"); 
     JLabel sumOfAmountsTitle = new JLabel("Sum of Amounts:"); 
     JLabel fileTypeTitle = new JLabel("Camt54 File Type:"); 

     JTextField groupMessageIdText = new JTextField("",10); 
     JTextField isoDateTimeText = new JTextField("",10); 
     JTextField notificationIdText = new JTextField("",10); 
     JTextField notificationAcctIdText = new JTextField("",10); 
     JTextField numberOfEntriesText = new JTextField("",10); 
     JTextField sumOfAmountsText = new JTextField("",10); 

     String[] fileTypes = {"OTC-R Message", "Home-Banking", "Cleared Checks"}; 

     JComboBox fileTypesComboBox = new JComboBox(fileTypes); 

     panelLine1.add(groupMessageIdTitle); 
     panelLine1.add(groupMessageIdText); 
     panelLine1.add(isoDateTimeTitle); 
     panelLine1.add(isoDateTimeText); 
     panelLine2.add(notificationIdTitle); 
     panelLine2.add(notificationIdText); 
     panelLine2.add(notificationAcctIdTitle); 
     panelLine2.add(notificationAcctIdText); 
     panelLine3.add(numberOfEntriesTitle); 
     panelLine3.add(numberOfEntriesText); 
     panelLine3.add(sumOfAmountsTitle); 
     panelLine3.add(sumOfAmountsText); 
     panelLine4.add(fileTypeTitle); 
     panelLine4.add(fileTypesComboBox); 
     panelLine5.add(confirmButton); 

     panelOne.add(panelLine1,BorderLayout.NORTH); 
     panelOne.add(panelLine2,BorderLayout.CENTER); 
     panelOne.add(panelLine3,BorderLayout.SOUTH); 
     mainPanel.add(panelOne,BorderLayout.NORTH); 
     mainPanel.add(panelLine4,BorderLayout.CENTER); 
     mainPanel.add(panelLine5,BorderLayout.SOUTH); 

     frame.add(mainPanel); 

     frame.setVisible(true); 

     frame.setSize(630,210); 

     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

    } 

} 
+0

これは意見に基づくものであり、StackOverflowのトピックではありません –

+0

これは完全に意見に基づいているとは思われません。優れたコードの記述は、十分に文書化されています。私はよいコード作成に関するより多くの経験を持つ人を求めています。これを行うより良い方法があるかどうか私に教えてください。私は、意見だけでなく経験に基づいて助けを求めています。 – jesric1029

+0

コードデザインがプロフェッショナルに見えるかどうかを確認しています。人々が「専門家」として定義できるのは、意見に基づくものです。あなたのコードを改善する方法のヒントを人々に提供したいと思うように思えます。そのためには[CodeReview](http://codereview.stackexchange.com)にアクセスしてください。 StackOverflowはあなたが*特定の懸念を持っているときのためのものです –

答えて

2

私はそれが意見だと思いますが、私は、このGUIは、より本格的に見えると思います。

All File Types

ラベルとテキストフィールドが列に並んでいます。 GridBagLayoutを使ってGUIのフォーム部分を作成しました。

ここにコードがあります。 JFrameコード、JPanelコード、およびJPanel内のSwingコンポーネントをグループ化して、コードを分かりやすくしました。

私はこのコードでメインメソッドを配置したので、貼り付けるファイルは1つだけでした。

package com.ggl.testing; 

import java.awt.BorderLayout; 
import java.awt.Component; 
import java.awt.Container; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 

import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 

public class AllGUI { 

    private static final Insets normalInsets = new Insets(10, 10, 0, 10); 

    public static void main(String[] args) { 
     Runnable runnable = new Runnable() { 
      @Override 
      public void run() { 
       new AllGUI().createAllGUI(); 
      } 
     }; 
     SwingUtilities.invokeLater(runnable); 
    } 

    public void createAllGUI() { 
     JFrame frame = new JFrame("All File Types Selection"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     frame.add(createMainPanel()); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    private JPanel createMainPanel() { 
     JPanel mainPanel = new JPanel(new BorderLayout()); 

     JPanel formPanel = new JPanel(new GridBagLayout()); 

     int gridy = 0; 

     JLabel groupMessageIdTitle = new JLabel("Group Message Id:"); 
     addComponent(formPanel, groupMessageIdTitle, 0, gridy, 1, 1, 
       normalInsets, GridBagConstraints.LINE_START, 
       GridBagConstraints.HORIZONTAL); 

     JTextField groupMessageIdText = new JTextField("", 10); 
     addComponent(formPanel, groupMessageIdText, 1, gridy, 1, 1, 
       normalInsets, GridBagConstraints.LINE_START, 
       GridBagConstraints.HORIZONTAL); 

     JLabel isoDateTimeTitle = new JLabel("ISO Creation Date/Time:"); 
     addComponent(formPanel, isoDateTimeTitle, 2, gridy, 1, 1, normalInsets, 
       GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL); 

     JTextField isoDateTimeText = new JTextField("", 10); 
     addComponent(formPanel, isoDateTimeText, 3, gridy++, 1, 1, 
       normalInsets, GridBagConstraints.LINE_START, 
       GridBagConstraints.HORIZONTAL); 

     JLabel notificationIdTitle = new JLabel("Notification Id:"); 
     addComponent(formPanel, notificationIdTitle, 0, gridy, 1, 1, 
       normalInsets, GridBagConstraints.LINE_START, 
       GridBagConstraints.HORIZONTAL); 

     JTextField notificationIdText = new JTextField("", 10); 
     addComponent(formPanel, notificationIdText, 1, gridy, 1, 1, 
       normalInsets, GridBagConstraints.LINE_START, 
       GridBagConstraints.HORIZONTAL); 

     JLabel notificationAcctIdTitle = new JLabel("Notification Account Id:"); 
     addComponent(formPanel, notificationAcctIdTitle, 2, gridy, 1, 1, 
       normalInsets, GridBagConstraints.LINE_START, 
       GridBagConstraints.HORIZONTAL); 

     JTextField notificationAcctIdText = new JTextField("", 10); 
     addComponent(formPanel, notificationAcctIdText, 3, gridy++, 1, 1, 
       normalInsets, GridBagConstraints.LINE_START, 
       GridBagConstraints.HORIZONTAL); 

     JLabel numberOfEntriesTitle = new JLabel("Number of Entries:"); 
     addComponent(formPanel, numberOfEntriesTitle, 0, gridy, 1, 1, 
       normalInsets, GridBagConstraints.LINE_START, 
       GridBagConstraints.HORIZONTAL); 

     JTextField numberOfEntriesText = new JTextField("", 10); 
     addComponent(formPanel, numberOfEntriesText, 1, gridy, 1, 1, 
       normalInsets, GridBagConstraints.LINE_START, 
       GridBagConstraints.HORIZONTAL); 

     JLabel sumOfAmountsTitle = new JLabel("Sum of Amounts:"); 
     addComponent(formPanel, sumOfAmountsTitle, 2, gridy, 1, 1, 
       normalInsets, GridBagConstraints.LINE_START, 
       GridBagConstraints.HORIZONTAL); 

     JTextField sumOfAmountsText = new JTextField("", 10); 
     addComponent(formPanel, sumOfAmountsText, 3, gridy++, 1, 1, 
       normalInsets, GridBagConstraints.LINE_START, 
       GridBagConstraints.HORIZONTAL); 

     JLabel fileTypeTitle = new JLabel("Camt54 File Type:"); 
     addComponent(formPanel, fileTypeTitle, 0, gridy, 1, 1, normalInsets, 
       GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL); 

     String[] fileTypes = { "OTC-R Message", "Home-Banking", 
       "Cleared Checks" }; 
     JComboBox<String> fileTypesComboBox = new JComboBox<>(fileTypes); 
     addComponent(formPanel, fileTypesComboBox, 1, gridy, 1, 1, 
       normalInsets, GridBagConstraints.LINE_START, 
       GridBagConstraints.HORIZONTAL); 

     JPanel confirmPanel = new JPanel(); 
     JButton confirmButton = new JButton("Confirm"); 
     confirmPanel.add(confirmButton); 

     mainPanel.add(formPanel, BorderLayout.CENTER); 
     mainPanel.add(confirmPanel, BorderLayout.SOUTH); 

     return mainPanel; 
    } 

    private void addComponent(Container container, Component component, 
      int gridx, int gridy, int gridwidth, int gridheight, Insets insets, 
      int anchor, int fill) { 
     GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, 
       gridwidth, gridheight, 0.0D, 0.0D, anchor, fill, insets, 0, 0); 
     container.add(component, gbc); 
    } 

} 
+0

ありがとうございます。以前はGridBagを使ってみたことがありましたが、あまり運がなかったので、チュートリアルのコンセプトを実際の作業プロジェクトに入れるのに問題がありました。私はこれで私はそれを拡張し、それが私のために働くことができると思います。これははるかに良く見え、私は行くべき最善の方法だと思う。私はあなたの助けを非常に感謝します! – jesric1029

関連する問題