2016-04-08 20 views
-1

Şu anda 6 farklı GUI oluşturmamı gerektiren bir projeyi geliştirmek için çalışıyorum. GUI tasarımında çok kötü olduğumu söyleyerek başlamalıyım. Izgara Çantası'nı öğrenmeye çalıştım ama gerçekten zor buldu.JTextArea projesi için daha verimli GUI tasarımı

Bu GUI'de BorderLayout ve Flow Layout kullanıyorum ve iyi çalışıyor. GUI'leri oluşturabilir ve hedefe ulaşabilirim ancak bu yöntemin profesyonel olmadığını ve diğer GUI'lerin daha fazla JTextFields ve JLabels (bazılarının da irade).

Burada sahip olduğum panellerin miktarını azaltabilecek herhangi bir düzen olup olmadığını bilmek istiyorum. Izgara düzenlerini araştırdım ama tüm hücrelerin aynı boyutta olmasıyla ilgili endişelerim var. Birisi bana, bu projenin en iyi düzenini ve deneyimlerinden haberdar ederek yardımcı olabilirse ve bana nasıl kullanılacağını gösterebilirse bunu takdir ediyorum. GridBag mizanpajı gibi mizanpaj dersleri yapmayı denedim ama aslında kendi projelerimde uygulayacağım problemler var.

Ana Sınıfı

package mainClasses; 

import gui.AllGUI; 

public class TesterClass 

{ 

    public static void main(String args[]) 

    { 

     AllGUI guiALL = new AllGUI(); 
     guiALL.createAllGUI(); 



    } 

} 

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

Bu görüşlere dayanan, bu tamamen görüş dayalı olarak kabul edilir nasıl görmüyorum konu dışı StackOverflow'daki –

+0

için olmasıdır. İyi kod yazımı iyi belgelenmiş ve bilinir. Bunu yapmak için daha iyi bir yol olup olmadığını bana bildirmek için iyi kod yazımı konusunda daha fazla deneyime sahip birisini soruyorum - sadece fikirleri değil, deneyime dayanarak yardım istiyorum. – jesric1029

+0

Kod tasarımının profesyonel görünüp görünmediğini soruyorsunuz. İnsanların "profesyonel" olarak tanımlayabilecekleri kanaat temellidir. İnsanların kodunuzu nasıl geliştireceğiniz konusunda size ipuçları vermesini istediğiniz gibi görünüyor. Bunun için [CodeReview] (http://codereview.stackexchange.com) adresine gitmelisiniz. StackOverflow, * belirli bir * endişeniz olduğunda geçerlidir. –

cevap

2

Ben bir görüş olduğunu tahmin, ama bu GUI daha profesyonel bir görünüme düşünüyorum.

All File Types

etiketler ve metin alanları sütunlarda hizalanmış. GUI'nin form bölümünü oluşturmak için GridBagLayout'u kullandım.

İşte kod. Kodun daha kolay anlaşılabilmesi için JFrame kodunu, JPanel kodunu ve JPanel içindeki Swing bileşenlerini birlikte gruplandırdım.

Bu kodu kullanarak ana yöntemi koydum, böylece yalnızca bir dosyanın yapıştırılmasını istiyorum.

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

Çok teşekkür ederim. GridBag'ı daha önce kullanmayı denedim ama çok şansım olmadı, eğitim konseptlerini gerçek bir iş projesine sokarken sorun yaşadım. Sanırım bunun üzerinde genişleyebilir ve benim için çalışabilirim. Bu daha iyi görünüyor ve bence en iyi yol. Yardımını çok takdir ediyorum! – jesric1029

İlgili konular