当我尝试在框架上添加多个组件时,摇摆显示白框

时间:2018-12-09 20:43:57

标签: java swing

我正在开发Java swing应用程序。我尝试在主JFrame上添加2个JPanel。

我的主要JPanel类:

public class ChatClient2 implements Runnable {
private Socket socket = null;
private Thread thread = null;
private DataInputStream console = null;
private DataOutputStream streamOut = null;
private ChatClientThread client = null;

public ChatClient2(String serverName, int serverPort) {
    Scanner in = new Scanner(System.in);
    System.out.println("Please enter your name:");
    name = in.nextLine();

    System.out.println("Establishing connection. Please wait ...");
    try {
        socket = new Socket(serverName, serverPort);
        System.out.println("Connected: " + socket);
        start();
    } catch (UnknownHostException uhe) {
        System.out.println("Host unknown: " + uhe.getMessage());
    } catch (IOException ioe) {
        System.out.println("Unexpected exception: " + ioe.getMessage());
    }
}

public void run() {
    while (thread != null) {
        try {
            streamOut.writeUTF(console.readLine());
            streamOut.flush();
        } catch (IOException ioe) {
            System.out.println("Sending error: " + ioe.getMessage());
            stop();
        }
    }
}

public void handle(String msg) {
    if (msg.equals(".bye")) {
        System.out.println("Good bye. Press RETURN to exit ...");
        stop();
    } else
        System.out.println(msg);
}

public void start() throws IOException {
    console = new DataInputStream(System.in);
    streamOut = new DataOutputStream(socket.getOutputStream());
    if (thread == null) {
        client = new ChatClientThread(this, socket);
        thread = new Thread(this);
        thread.start();
    }
}

public void stop() {
    if (thread != null) {
        thread.stop();
        thread = null;
    }
    try {
        if (console != null)
            console.close();
        if (streamOut != null)
            streamOut.close();
        if (socket != null)
            socket.close();
    } catch (IOException ioe) {
        System.out.println("Error closing ...");
    }
    client.close();
    client.stop();
}

public static void main(String args[]) {
    ChatClient2 client = null;
    if (args.length != 2)
        System.out.println("Usage: java ChatClient host port");
    else
        client = new ChatClient2(args[0], Integer.parseInt(args[1]));
}
当我添加为主机上的唯一组件时,

工作正常。但是,当我尝试在框架上添加另一个JPanel时,我的DrawingPanel显示为小白框。

public class ChatClientThread extends Thread {
private Socket socket = null;
private ChatClient2 client = null;
private DataInputStream streamIn = null;

public ChatClientThread(ChatClient2 _client, Socket _socket) {
    client = _client;
    socket = _socket;
    open();
    start();
}

public void open() {
    try {
        streamIn = new DataInputStream(socket.getInputStream());
    } catch (IOException ioe) {
        System.out.println("Error getting input stream: " + ioe);
        client.stop();
    }
}

public void close() {
    try {
        if (streamIn != null)
            streamIn.close();
    } catch (IOException ioe) {
        System.out.println("Error closing input stream: " + ioe);
    }
}

public void run() {
    while (true) {
        try {
            client.handle(streamIn.readUTF());
        } catch (IOException ioe) {
            System.out.println("Listening error: " + ioe.getMessage());
            client.stop();
        }
    }
}

看起来像这样

result

我尝试按照JPanel appears as a small white box问题中的建议,将getPreferredSize()方法添加到DrawingPanel中,但是效果不佳。

您能帮我解决这个问题吗?

更新:

我已经更改了布局,现在我看到了我的JPanel的一半。我认为问题与JPanels和JFrame的固定大小有关。会调查的。

update

2 个答案:

答案 0 :(得分:1)

因此,我拿走了您的上下文外代码,将一个可运行的变体拖在一起,并且...没有问题。这表明问题出在您未向我们显示的代码中。而不是发布不完整代码的“片段”(这会引发更多问题),而是发布Minimal, Complete, and Verifiable example(可以编译并运行),以演示您遇到的问题

一个可以运行并运行的示例...

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Test {

  public static void main(String[] args) {
    new Test();
  }

  public Test() {
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        DrawingPanel panel = new DrawingPanel();

        JFrame frame = new JFrame("Hex Testing 4");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = frame.getContentPane();
        JPanel aggregationFrame = new JPanel(new GridBagLayout());
        aggregationFrame.add(panel);
        aggregationFrame.add(new JLabel("Enter username:"));
        content.add(aggregationFrame);
        //this.add(panel);  -- cannot be done in a static context
        //for hexes in the FLAT orientation, the height of a 10x10 grid is 1.1764 * the width. (from h / (s+t))
        frame.pack();
        //frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    });
  }

  public class DrawingPanel extends JPanel {
    //mouse variables here
    //Point mPt = new Point(0,0);

    private int width = 200;
    private int height = 200;

    public DrawingPanel() {
      setBackground(Color.BLACK);
//    MyMouseListener ml = new MyMouseListener();
//    addMouseListener(ml);
    }

    public Dimension getPreferredSize() {
      return new Dimension(width, height);
    }
  }

}

答案 1 :(得分:0)

问题出在我的组件尺寸上。好像它不适合主机架并被切割了。我设置了动态帧大小,现在效果很好。