在Windows 7平板电脑中执行时,我的GUI看起来很奇怪

时间:2012-09-18 15:12:48

标签: java swing user-interface gridbaglayout

最近我用JAVA / Swing编写了一对带有简单GUI的应用程序:

  • 一个JScrollPane
  • 在第一个应用程序中,JScrollPane中有一个JTextArea,第二个应用程序中有一个JTable。

他们看起来很好,他们工作正常。直到我在Windows 7平板电脑(HP Slate)上执行。它们看起来都是空的,中间有一个小符号。此图显示了第一个应用程序(JTextArea one)在平板电脑上的显示效果:

enter image description here

这里有什么问题,如何解决?

我正在显示代码,但由于在使用计算机时看起来很好,我想这不是问题所在:

import java.io.*;
import java.net.*;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.text.DefaultCaret;

class EstacionBase extends JFrame{
    private static final long serialVersionUID = 1L;

private static final String logFile = "log.txt";

private DateFormat df;
private FileWriter logWriter;

private Socket sisnetSocket;
private ServerSocket serverSocket;
private Socket clientSocket;
private BufferedWriter outToPlane;
private BufferedWriter outToDataServer;
private BufferedReader inputFromDataServer;
private JTextArea console;

private boolean terminated;

public EstacionBase() {
    setTitle("Base station");
    createGUI();
    pack();
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);

    try{
        df = new SimpleDateFormat ("[hh:mm:ss]");
        logWriter = new FileWriter(System.getProperty("user.dir") + "\\" + logFile, true);
        serverSocket = new ServerSocket(PORT); // A socket I use for listening for connections
    } catch(IOException e){
        JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }

    //mainLoop(); // doing some stuff

    try{
        logWriter.close();
        serverSocket.close();
    } catch(IOException e){
        JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}

private void createGUI() {
    setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.fill = GridBagConstraints.BOTH;

    console = new JTextArea(40, 80);
    JScrollPane scroll = new JScrollPane(console);
    DefaultCaret caret = (DefaultCaret) console.getCaret();
    caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); // Autoscroll
    console.setText("Welcome to base station");
    console.setEditable(false);
    console.setVisible(true);
    add(scroll, c);
}

private void writeToConsole(String msg) throws IOException{
    Date now = new Date();
    String timeStr = df.format(now);
    console.append("\n");
    console.append(timeStr + " " + msg);
    writeToLogFile(msg, timeStr);
}

private void writeToLogFile(String msg, String timeStr) throws IOException{
    logWriter.write(timeStr + " " + msg + CRLF);
        logWriter.flush();
    }

    public static void main(String[] arg) {
        EstacionBase frame = new EstacionBase();
    }

}

1 个答案:

答案 0 :(得分:2)

这是一个GridBagLayout问题;我不确定最佳修复方法,但它可以使用默认的JFrame布局BorderLayout.CENTER。从功能上讲,这三种布局具有类似的效果:

  • BorderLayout.CENTERJFrame#add()的默认设置。

  • GridLayout():一列,一行,无间隙。

  • GridBagLayout():默认GridBagConstraints()将所有字​​段设置为默认值。

另外,应该在event dispatch thread上构建和操作的Swing GUI对象。