我的JList占据了整个屏幕

时间:2014-01-19 21:42:22

标签: java swing jframe jscrollpane jlist

我的Java程序中的JList占用了整个JPanel! 我该怎么办?

package infoKupProcess;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

class TCPServer extends JFrame {
    public static String processes = "none";
    static String processesList[] = processes.split(":");
    private static int screenWidth = 800, screenHeight = 600;
    static JButton quitButton;
    static JScrollPane procScroll;
    static ServerSocket inSocket;
    private static boolean running = false;

    private static JList procList;

    public TCPServer() {
        initUI();
    }

    public void initUI() {
        JPanel panel = new JPanel();
        getContentPane().add(panel);
        panel.setLayout(new BorderLayout());
        setSize(800, 600);
        // {
        // String listData[] = { "Item 1", "Item 2", "Item 3", "Item 4" };
        //
        // // Create a new listbox control
        // procList = new JList(listData);
        // panel.add(procList, BorderLayout.CENTER);
        // }
        {
            quitButton = new JButton("Ugasi");
            quitButton.setBounds(705, 533, 80, 30);

            quitButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent event) {
                    running = false;
                    try {
                        inSocket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    System.exit(0);
                }
            });
            panel.add(quitButton);
        }
        // setTitle("TCPServer");
        // setSize(screenWidth, screenHeight);
        // setLocationRelativeTo(null);
        // setDefaultCloseOperation(EXIT_ON_CLOSE);
        // Set the frame characteristics

        procList = new JList();
        procList.setListData(processesList);
        procScroll = new JScrollPane(procList);
        panel.add(procScroll);
    }

    public static void main(String argv[]) throws Exception {
        running = true;
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TCPServer ex = new TCPServer();
                ex.setVisible(true);
            }
        });
        processProcesses();
    }

    public static void processProcesses() throws Exception {
        String clientSentence, capitalizedSentence;
        inSocket = new ServerSocket(25565);
        while (running) {
            Socket connectionSocket = inSocket.accept();
            BufferedReader inFromClient = new BufferedReader(
                    new InputStreamReader(connectionSocket.getInputStream()));

            clientSentence = inFromClient.readLine();
            processes = clientSentence;
            processesList=processes.split(":");
            procList.setListData(processesList);
            DataOutputStream outToClient = new DataOutputStream(
                    connectionSocket.getOutputStream());
            capitalizedSentence = clientSentence.toUpperCase() + '\n';
            outToClient.writeBytes(capitalizedSentence);
        }
    }
}

2 个答案:

答案 0 :(得分:3)

您正在使用JPanel的默认BorderLayout.CENTER位置将列表的JScrollPane添加到BorderLayout。所以它将填补JPanel。解决方案:使用另一种布局,或者更好的是,使用JPanel将BorderLayout放置在另一个使用合适布局的JPanel中。


顺便说一下,其他问题:

  • 您的代码显示了静态修饰符的显着过度使用和误用,实际上您的大多数字段和方法都不应该是静态的。
  • 您不应该设置任何尺寸,首选尺寸,位置或范围。让组件,容器和布局管理器发挥作用,并设置GUI的大小。
  • 这可以通过在将其设置为可见之前调用JFrame上的pack()来完成。

答案 1 :(得分:0)

这可以解决您的问题,但我建议您使用正确的Layout Manager

替换

panel.add(procScroll);

通过

procScroll.setPreferredSize(new Dimension(150,80));
JPanel panel1=new JPanel();
panel1.add(procScroll);
panel.add(panel1);