想做一个好看些的界面。但是现在有些地方显示不正常。主要为程序上方得输入框和及文字并没有充满整个frame,而只是居中设置得。请各位看看如何调整。谢谢~~源代码如下:由于字数限制,分成两个帖子。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;public class Viewer
{
    // static fields:
    private static final String VERSION = "Version 3.1";
    private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));    // fields:
    private JFrame frame;
    private NetworkImage imagePanel;
    private JLabel filenameLabel;
    private JLabel statusLabel;
    private JButton smallerButton;
    private JButton largerButton;
    private JButton mouse;
    private JTextField startVertex;
    private JTextField communitySize;
    private JProgressBar progressBar;
    private JButton startRun;
    
    /**
     * Create an ImageViewer and display its GUI on screen.
     */
    public Viewer()
    {
        makeFrame();
    }
        // ---- implementation of menu functions ----
    
    /**
     * Open function: open a file chooser to select a new image file,
     * and then display the chosen image.
     */
    private void openFile()
    {
     System.out.println("openFile");
        setButtonsEnabled(true);
//        showFilename(selectedFile.getPath());
        showStatus("File loaded.");
        frame.pack();
    }    /**
     * Close function: close the current image.
     */
    private void close()
    {
        System.out.println("Close");
        setButtonsEnabled(false);
    }    /**
     * Save As function: save the current image to a file.
     */
    private void saveAs()
    {
     System.out.println("saveAs");
    }    /**
     * Quit function: quit the application.
     */
    private void quit()
    {
        System.exit(0);
    }
    
    private void showAbout()
    {
        JOptionPane.showMessageDialog(frame, 
                    "Local Community Finder\n " +
                    "sdqwdswqdqwqw\n" +
                    "wsdqwqwqwqwwdsq\n" +
                    "qwdqwdwqdqwdqw\n" +
                     VERSION,
                    "About Helpwswswqswsw\n",
                    JOptionPane.INFORMATION_MESSAGE);

解决方案 »

  1.   

        }
        
        /**
         * Show a message in the status bar at the bottom of the screen.
         * @param text The status message.
         */
        private void showStatus(String text)
        {
            statusLabel.setText(text);
        }
        
        
        /**
         * Enable or disable all toolbar buttons.
         * 
         * @param status  'true' to enable the buttons, 'false' to disable.
         */
        private void setButtonsEnabled(boolean status)
        {
            smallerButton.setEnabled(status);
            largerButton.setEnabled(status);
        }
        
        // ---- Swing stuff to build the frame and all its components and menus ----
        
        /**
         * Create the Swing frame and its content.
         */
        private void makeFrame()
        {
            frame = new JFrame("Local Community Finder");
            JPanel contentPane = (JPanel)frame.getContentPane();
            contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));        makeMenuBar(frame);
            
            // Specify the layout manager with nice spacing
            contentPane.setLayout(new BorderLayout(6, 6));
            
            // Create the image pane in the center
            imagePanel = new NetworkImage();
            imagePanel.setBorder(new EtchedBorder());
            contentPane.add(imagePanel, BorderLayout.CENTER);        // Create two menua and some necessary information
    //        filenameLabel = new JLabel();
    //        contentPane.add(filenameLabel, BorderLayout.NORTH);
            
            //create the search and relevant text field
            JPanel searchPanel = new JPanel();
            GridBagConstraints constraints;
            GridBagLayout layout = new GridBagLayout();
            searchPanel.setLayout(layout);        JLabel startLabel = new JLabel("Start Vertex:");
            constraints = new GridBagConstraints();
            constraints.anchor = GridBagConstraints.EAST;
            constraints.insets = new Insets(5, 5, 0, 0);
            layout.setConstraints(startLabel, constraints);
            searchPanel.add(startLabel);        startVertex = new JTextField();
            constraints = new GridBagConstraints();
            constraints.fill = GridBagConstraints.HORIZONTAL;
            constraints.gridwidth = GridBagConstraints.REMAINDER;
            constraints.insets = new Insets(5, 5, 0, 5);
            layout.setConstraints(startVertex, constraints);
            searchPanel.add(startVertex);        JLabel maxLabel = new JLabel("Max Commmunity Size:");
            constraints = new GridBagConstraints();
            constraints.anchor = GridBagConstraints.EAST;
            constraints.insets = new Insets(5, 5, 0, 0);
            layout.setConstraints(maxLabel, constraints);
            searchPanel.add(maxLabel);        communitySize = new JTextField();
            constraints = new GridBagConstraints();
            constraints.fill = GridBagConstraints.HORIZONTAL;
            constraints.gridwidth = GridBagConstraints.REMAINDER;
            constraints.insets = new Insets(5, 5, 0, 5);
            layout.setConstraints(communitySize, constraints);
            searchPanel.add(communitySize);
            
            JLabel progressLabel = new JLabel("Community Detecting Progress:");
            constraints = new GridBagConstraints();
            constraints.anchor = GridBagConstraints.EAST;
            constraints.insets = new Insets(5, 5, 0, 0);
            layout.setConstraints(progressLabel, constraints);
            searchPanel.add(progressLabel);
            
            progressBar = new JProgressBar();
            progressBar.setMinimum(0);
            progressBar.setStringPainted(true);
            constraints = new GridBagConstraints();
            constraints.fill = GridBagConstraints.HORIZONTAL;
            constraints.gridwidth = GridBagConstraints.REMAINDER;
            constraints.insets = new Insets(5, 5, 0, 5);
            layout.setConstraints(progressBar, constraints);
            searchPanel.add(progressBar);
            
            startRun = new JButton("Search");
            startRun.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                actionSearch();
              }
            });
            constraints = new GridBagConstraints();
            constraints.gridwidth = GridBagConstraints.REMAINDER;
            constraints.insets = new Insets(5, 5, 5, 5);
            layout.setConstraints(startRun, constraints);
            searchPanel.add(startRun);
            
            contentPane.add(searchPanel, BorderLayout.NORTH);
            
            JSeparator separator = new JSeparator();
            constraints = new GridBagConstraints();
            constraints.fill = GridBagConstraints.HORIZONTAL;
            constraints.gridwidth = GridBagConstraints.REMAINDER;
            constraints.insets = new Insets(5, 5, 5, 5);
            layout.setConstraints(separator, constraints);
            searchPanel.add(separator);
            
            statusLabel = new JLabel(VERSION);
            contentPane.add(statusLabel, BorderLayout.SOUTH);
            
            // Create the toolbar with the buttons
            JPanel toolbar = new JPanel();
            toolbar.setLayout(new GridLayout(0, 1));
            
            smallerButton = new JButton("Smaller");
            smallerButton.addActionListener(new ActionListener() {
                                   public void actionPerformed(ActionEvent e) { makeSmaller(); }
                               });
            toolbar.add(smallerButton);
            
            largerButton = new JButton("Larger");
            largerButton.addActionListener(new ActionListener() {
                                   public void actionPerformed(ActionEvent e) { makeLarger(); }
                               });
            toolbar.add(largerButton);
            
                    // Add toolbar into panel with flow layout for spacing
            JPanel flow = new JPanel();
            flow.add(toolbar);
            
            contentPane.add(flow, BorderLayout.WEST);
            
            setButtonsEnabled(false);
            frame.pack();
            
            // place the frame at the center of the screen and show
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
            frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
            frame.setVisible(true);
        }
        
        /**
         * Create the main frame's menu bar.
         * 
         * @param frame   The frame that the menu bar should be added to.
         */
        private void makeMenuBar(JFrame frame)
        {
            final int SHORTCUT_MASK =
                Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();        JMenuBar menubar = new JMenuBar();
            frame.setJMenuBar(menubar);
            
            JMenu menu;
            JMenuItem item;
            
            // create the File menu
            menu = new JMenu("Control");
            menubar.add(menu);
            
            item = new JMenuItem("Open Result File");
                item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, SHORTCUT_MASK));
                item.addActionListener(new ActionListener() {
                                   public void actionPerformed(ActionEvent e) { openFile(); }
                               });
            menu.add(item);        item = new JMenuItem("Close");
                item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, SHORTCUT_MASK));
                item.addActionListener(new ActionListener() {
                                   public void actionPerformed(ActionEvent e) { close(); }
                               });
            menu.add(item);
            menu.addSeparator();
            
            item = new JMenuItem("Quit");
                item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
                item.addActionListener(new ActionListener() {
                                   public void actionPerformed(ActionEvent e) { quit(); }
                               });
            menu.add(item);        // create the Help menu
            menu = new JMenu("Help");
            menubar.add(menu);
            
            item = new JMenuItem("About Community Finder...");
                item.addActionListener(new ActionListener() {
                                   public void actionPerformed(ActionEvent e) { showAbout(); }
                               });
            menu.add(item);
        }
        
        private void actionSearch(){
        
        }
        private void makeSmaller(){
         System.out.println("MakeSmaller");
        }
        
        private void makeLarger(){
         System.out.println("makeLarger");
         }
        
        public static void main(String args[]){
         Viewer image = new Viewer();
        }
    }
      

  2.   

    我已经设置了布局了。使用BorderLayout。但是很奇怪的就是上部的信息没有扩展到整个界面中/