2012-04-10 13 views
4

私はSwingとAWTを使ってJava GUIを手作業でコーディングしようとしています。私は以下の投稿をGUIに似た何かを試してみて、達成またはために、様々なレイアウトを使用しています(それは鉛筆で作られたモックレイアウトです):私が得たものハンドコーディングされたGUI - Java

Mock Layout - (Made with Pencil)

がこれまでにですが、見えることはできませんできるだけ丁寧で、魅力的で、使いやすいようにします。

Java GUI try

これは私がこれまで行っているコードです:

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


public class GUI extends JFrame { 

    public void buildGui() { 

     JFrame frame = new JFrame("Hotel TV Scheduler"); 

       JPanel mainPanel = new JPanel(); 
     mainPanel.setLayout(new BorderLayout(0,0)); 

       JPanel chPanel = new JPanel(); 
       chPanel.setLayout(new GridLayout(3,2)); 

       JPanel listPanel = new JPanel(); 
       listPanel.setLayout(new GridLayout(3,2)); 

       JPanel infoPanel = new JPanel(); 
     infoPanel.setLayout(new GridLayout(0,2)); 


       JPanel addPanel = new JPanel(); 
     addPanel.setLayout(new GridLayout(0,3)); 


       mainPanel.add(chPanel, BorderLayout.LINE_START); 
       mainPanel.add(listPanel, BorderLayout.CENTER); 
       mainPanel.add(infoPanel, BorderLayout.LINE_END); 


       JTable chOneTable = new JTable(); 
       JTable chTwoTable = new JTable(); 
       JTable listTable = new JTable(); 

       JLabel ch1Label = new JLabel("Channel 1"); 
       JLabel ch2Label = new JLabel("Channel 2"); 
       JLabel listLabel = new JLabel("List"); 

       JButton rmvChOneButton = new JButton("Remove Channel"); 
       JButton rmvChTwoButton = new JButton("Remove Channel"); 

       chPanel.add(ch1Label); 
       chPanel.add(ch2Label); 
       chPanel.add(chOneTable); 
       chPanel.add(chTwoTable); 
       chPanel.add(rmvChOneButton);           
       chPanel.add(rmvChTwoButton); 

       listPanel.add(listLabel); 
       listPanel.add(listTable);     



       JLabel titleLabel = new JLabel("Title"); 
       JLabel genreLabel = new JLabel("Genre"); 
       JLabel durationLabel = new JLabel("Duration"); 
       JLabel actorLabel = new JLabel("Actor"); 
       JLabel directorLabel = new JLabel("Director"); 
       JLabel rentableLabel = new JLabel("Rentable"); 
       JLabel synLabel = new JLabel("Synopsis"); 

       JTextField txtTitle = new JTextField();   
       JTextField txtGenre = new JTextField(); 
       JTextField txtDuration = new JTextField(); 
       JTextField txtActor = new JTextField(); 
       JTextField txtDirector = new JTextField(); 
       JTextField txtSynopsis = new JTextField(); 

       JCheckBox rentCB = new JCheckBox(); 

       JButton btnAddProg = new JButton("Add Program"); 

       JList channelList = new JList(); 
       JList timeList = new JList(); 

       infoPanel.add(titleLabel); 
       infoPanel.add(txtTitle); 
       infoPanel.add(genreLabel); 
       infoPanel.add(txtGenre); 
       infoPanel.add(durationLabel); 
       infoPanel.add(txtDuration); 
       infoPanel.add(actorLabel); 
       infoPanel.add(txtActor); 
       infoPanel.add(directorLabel); 
       infoPanel.add(txtDirector); 
       infoPanel.add(rentableLabel); 
       infoPanel.add(rentCB); 
       infoPanel.add(synLabel); 
       infoPanel.add(txtSynopsis); 
       infoPanel.add(btnAddProg); 
       infoPanel.add(channelList); 
       infoPanel.add(timeList); 


       frame.add(mainPanel); 
       frame.setVisible(true); 


    } 


} 

をそれは正確に上に示したモックのレイアウトのように持っていますが、同様の、あるいは少なくともそれ以上できるだけ多くはありません使いやすい。

GridBagLayoutとSwingLayout以外は使用したいです。

コードを改善してより似ているようにするためのアイデアはありますか?

何か助けていただければ幸いです。

ブライアン

+0

GridBagLayoutのこちらをご覧ください。それは本当にここに物事を助けるでしょう。私はその難しいことを知っているが、弾丸をかむ。 – Starkey

+0

実際には学校の割り当てであり、GridBagLayoutを使用するGUIクリエイターが見つかるという単純な理由から、使用しないように要求されました。 – Brian

+0

一言...私がお勧めするのは、あなたが探している結果を得るために必要なだけレイアウトを入れ子にすることです。 – Starkey

答えて

2

MigLayoutをご覧ください。ライセンスは非常に包括的です。

MigLayoutは、商用および非商用プロジェクトには無料で使用でき、ソースコードが提供されています。あなたが偉大な例と対応するソースを表示する必要があり

JNLP demo applicationを好む方、非常に自由なBSDまたはGPLライセンスの下でライセンスされています。

また、論理的に無関係なコンポーネントをネストすることは避けてください。ネスティングの度合いを増やすと、整列、境界線、および詰め物の取得が非常に苦しくなります。

1

使用EclipseとWindowBuilder。必要に応じて、後で特定の部品を「手作業でコード」して、WindowBuilderに戻すことができます。

+0

Netbeansは非常にまともなGUIビルダーを持っていました。 Intellij IDEAにも1つしかありませんが、私はそれが有料版のためだと思います。 – mbatchkarov

2

何そのGUIはほとんど必要がある:コンポーネント間

  • ホワイトスペース。これを提供する一般的な方法は、次のとおりです。
    1. レイアウトのコンストラクタで提供されるレイアウトパディング。
    2. EmptyBorderをコンポーネントまたはコンテナに追加する。すでに罫線があるコンポーネントが多い場合は、JPanelにラップし、パネルに罫線を追加することをお勧めします。
  • 拘束パネル。例えば。 WESTBorderLayoutのコンポーネントのグループを「先頭に」表示することを希望する場合は、BorderLayout.NORTHレイアウト/制約の第1パネルを追加します。ここに私が意味するもののan exampleがある。

+0

@mKorbel私はまだ、「実際のLayoutManagerの達人」*がどこに隠れているのか不思議です。 ;) –

+0

+1の素晴らしい提案:-) – mKorbel

1

が、これは、私は本当にあなたがJButtonと「JSpinnerの」の間に使用しているかわからない

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.TitledBorder; 
import javax.swing.table.DefaultTableModel; 

public class TVSchedule 
{ 
    private static final int GAP = 5; 
    private static TVSchedule tvSchedule; 

    private void createAndDisplayGUI() 
    { 
     JFrame frame = new JFrame("HOTEL TV SCHEDULE"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.setLocationByPlatform(true); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new BorderLayout()); 

     JPanel centerPanel = new JPanel(); 
     //centerPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); 
     centerPanel.setLayout(new GridLayout(0, 4, 5, 5)); 
     centerPanel.add(createChannelOnePanel()); 
     centerPanel.add(createChannelTwoPanel()); 
     centerPanel.add(createListPanel()); 
     centerPanel.add(createInformationPanel()); 

     JPanel bottomPanel = new JPanel(); 
     bottomPanel.setOpaque(true); 
     bottomPanel.setBackground(Color.RED.darker()); 
     bottomPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); 
     JButton exitButton = new JButton("EXIT"); 
     bottomPanel.add(exitButton); 

     contentPane.add(centerPanel, BorderLayout.CENTER); 
     contentPane.add(bottomPanel, BorderLayout.PAGE_END); 

     frame.setContentPane(contentPane); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    private JPanel createChannelOnePanel() 
    { 
     JPanel panel = new JPanel(); 
     panel.setLayout(new GridBagLayout()); 
     panel.setOpaque(true); 
     panel.setBackground(Color.DARK_GRAY); 
     panel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP)); 
     GridBagConstraints gbc = new GridBagConstraints(); 

     String[] columnNames = { 
            "Time", 
            "Title" 
           }; 
     Object[][] data = { 
          {"01:00","Cowboy and Alchemist."} 
          }; 
     DefaultTableModel model = new DefaultTableModel(data, columnNames); 
     JTable table = new JTable(model) 
     { 
      // Returning the Class of each column will allow different 
      // renderers to be used based on Class 
      public Class getColumnClass(int column) 
      { 
       return getValueAt(0, column).getClass(); 
      } 
     };      

     table.setPreferredScrollableViewportSize(new Dimension(200, 200)); 
     table.setFillsViewportHeight(true); 
     JScrollPane scrollPane = new JScrollPane(table); 
     scrollPane.setBorder(BorderFactory.createTitledBorder(
         BorderFactory.createLineBorder(Color.BLACK, 1) 
         , "Channel 1" 
         , TitledBorder.CENTER 
         , TitledBorder.DEFAULT_POSITION));  
     gbc.weightx = 1.0; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.gridwidth = 2; 
     panel.add(scrollPane, gbc);  

     JButton removeButton = new JButton("Remove Selected"); 
     gbc.weightx = 1.0; 
     gbc.gridx = 0; 
     gbc.gridy = 3; 
     gbc.gridwidth = 2; 
     panel.add(removeButton, gbc); 

     return panel; 
    } 

    private JPanel createChannelTwoPanel() 
    { 
     JPanel panel = new JPanel(); 
     panel.setLayout(new GridBagLayout()); 
     panel.setOpaque(true); 
     panel.setBackground(Color.WHITE); 
     panel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP)); 
     GridBagConstraints gbc = new GridBagConstraints(); 

     String[] columnNames = { 
            "Time", 
            "Title" 
           }; 
     Object[][] data = { 
          {"02:00","Grey's Anatomy"} 
          }; 
     DefaultTableModel model = new DefaultTableModel(data, columnNames); 
     JTable table = new JTable(model) 
     { 
      // Returning the Class of each column will allow different 
      // renderers to be used based on Class 
      public Class getColumnClass(int column) 
      { 
       return getValueAt(0, column).getClass(); 
      } 
     };      

     table.setPreferredScrollableViewportSize(new Dimension(200, 200)); 
     table.setFillsViewportHeight(true); 
     JScrollPane scrollPane = new JScrollPane(table); 
     scrollPane.setBorder(BorderFactory.createTitledBorder(
         BorderFactory.createLineBorder(Color.BLACK, 1) 
         , "Channel 2" 
         , TitledBorder.CENTER 
         , TitledBorder.DEFAULT_POSITION));  
     gbc.weightx = 1.0; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.gridwidth = 2; 
     panel.add(scrollPane, gbc);  

     JButton removeButton = new JButton("Remove Selected"); 
     gbc.weightx = 1.0; 
     gbc.gridx = 0; 
     gbc.gridy = 3; 
     gbc.gridwidth = 2; 
     panel.add(removeButton, gbc); 

     return panel; 
    } 

    private JPanel createListPanel() 
    {  
     JPanel panel = new JPanel(); 
     panel.setLayout(new GridBagLayout()); 
     panel.setOpaque(true); 
     panel.setBackground(Color.DARK_GRAY); 
     panel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP)); 
     GridBagConstraints gbc = new GridBagConstraints(); 

     String[] columnNames = { 
            "Genre", 
            "Title", 
            "Duration (Hours)" 
           }; 
     Object[][] data = { 
          {"Comedy","C & A", "1.5"} 
          }; 
     DefaultTableModel model = new DefaultTableModel(data, columnNames); 
     JTable table = new JTable(model) 
     { 
      // Returning the Class of each column will allow different 
      // renderers to be used based on Class 
      public Class getColumnClass(int column) 
      { 
       return getValueAt(0, column).getClass(); 
      } 
     };      

     table.setPreferredScrollableViewportSize(new Dimension(200, 200)); 
     table.setFillsViewportHeight(true); 
     JScrollPane scrollPane = new JScrollPane(table); 
     scrollPane.setBorder(BorderFactory.createTitledBorder(
         BorderFactory.createLineBorder(Color.BLACK, 1) 
         , "List" 
         , TitledBorder.CENTER 
         , TitledBorder.DEFAULT_POSITION)); 
     gbc.weightx = 1.0; 
     gbc.anchor = GridBagConstraints.PAGE_START; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.gridwidth = 2; 
     panel.add(scrollPane, gbc); 

     gbc.weightx = 1.0; 
     gbc.gridx = 0; 
     gbc.gridy = 3; 
     gbc.gridwidth = 2; 
     panel.add(Box.createRigidArea(new Dimension(100, 30)), gbc);  

     return panel; 
    } 

    private JPanel createInformationPanel() 
    { 
     JPanel bottomPanel = new JPanel(); 
     bottomPanel.setLayout(new GridLayout(0, 1, 2, 2)); 
     bottomPanel.setBorder(BorderFactory.createTitledBorder(
         BorderFactory.createLineBorder(Color.BLACK, 1) 
         , "Information" 
         , TitledBorder.LEFT 
         , TitledBorder.DEFAULT_POSITION)); 

     JPanel panel = new JPanel(); 
     panel.setOpaque(true); 
     panel.setBackground(Color.WHITE); 
     panel.setLayout(new GridBagLayout()); 
     panel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP)); 
     GridBagConstraints gbc = new GridBagConstraints(); 

     JLabel titleLabel = new JLabel("TITLE : "); 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     panel.add(titleLabel, gbc); 

     JTextField titleField = new JTextField(10); 
     gbc.gridx = 1; 
     panel.add(titleField, gbc); 

     JLabel genreLabel = new JLabel("GENRE : "); 
     gbc.gridx = 0; 
     gbc.gridy = 1; 
     panel.add(genreLabel, gbc); 

     JTextField genreField = new JTextField(10); 
     gbc.gridx = 1; 
     panel.add(genreField, gbc); 

     JLabel durationLabel = new JLabel("DURATION : "); 
     gbc.gridx = 0; 
     gbc.gridy = 2; 
     panel.add(durationLabel, gbc); 

     JTextField durationField = new JTextField(10); 
     gbc.gridx = 1; 
     panel.add(durationField, gbc); 

     JLabel actorLabel = new JLabel("ACTOR : "); 
     gbc.gridx = 0; 
     gbc.gridy = 3; 
     panel.add(actorLabel, gbc); 

     JTextField actorField = new JTextField(10); 
     gbc.gridx = 1; 
     panel.add(actorField, gbc); 

     JLabel directorLabel = new JLabel("DIRECTOR : "); 
     gbc.gridx = 0; 
     gbc.gridy = 4; 
     panel.add(directorLabel, gbc); 

     JTextField directorField = new JTextField(10); 
     gbc.gridx = 1; 
     panel.add(directorField, gbc); 

     JLabel rentLabel = new JLabel("RENTABLE : "); 
     gbc.gridx = 0; 
     gbc.gridy = 5; 
     panel.add(rentLabel, gbc); 

     JCheckBox rentCBox = new JCheckBox(" ", false); 
     rentCBox.setOpaque(true); 
     rentCBox.setBackground(Color.WHITE); 
     rentCBox.setHorizontalTextPosition(SwingConstants.LEFT); 
     gbc.gridx = 1; 
     panel.add(rentCBox, gbc); 

     JLabel synopsisLabel = new JLabel("SYNOPSIS : "); 
     gbc.gridx = 0; 
     gbc.gridy = 6; 
     panel.add(synopsisLabel, gbc); 

     JTextArea synopsisArea = new JTextArea(10, 5); 
     synopsisArea.setBackground(Color.BLUE.darker()); 
     synopsisArea.setForeground(Color.WHITE); 
     synopsisArea.setCaretColor(Color.WHITE); 
     gbc.gridx = 1; 
     gbc.gridwidth = 2; 
     gbc.gridheight = 2; 
     panel.add(synopsisArea, gbc); 

     JButton addProgramButton = new JButton("Add Program"); 
     gbc.gridx = 0; 
     gbc.gridy = 8; 
     gbc.insets = new Insets(5, 5, 5, 5); 
     gbc.anchor = GridBagConstraints.PAGE_END; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 
     panel.add(addProgramButton, gbc); 

     JSpinner spinner = new JSpinner(new SpinnerNumberModel(00.15, 00.15, 60.00, 00.15)); 
     gbc.gridx = 2; 
     gbc.gridy = 8;  
     panel.add(spinner, gbc); 

     bottomPanel.add(panel); 
     return bottomPanel; 
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       tvSchedule = new TVSchedule(); 
       tvSchedule.createAndDisplayGUI(); 
      } 
     }); 
    } 
} 

:-)あなたのニーズを満たすために行います、このサンプルプログラムを見てください、それは何もそこに何も追加したことはありません、あなたはそれを自分で行うことができます願っていますここで

は同じのために出力されます:

HOTEL TV SCHEDULE

+0

+1ビデオオンデマンド:-) – mKorbel

+0

@mKorbel:Hehe、ありがとう:-) –