java - JFrame 加载故障
问题描述
有人可以帮我写代码吗?我在 Eclipse 中遇到了奇怪的行为。
有时我的应用程序加载正确,而大多数时候我没有正确加载 North 和 South JPanel 或根本没有加载。
加载 GUI 后,仅当我调整 JFrame 边框的大小时才能正确显示,但我想每次都正确加载。我想我的代码有问题,但我找不到。顺便说一句,我正在使用 java-11-openjdk-amd64 运行 Linux Mint 19.1 Tarra。这是 Main 类的代码:
package gui;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
new Dakijevstina();
}
}
这是 GUI 类代码:
package gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
@SuppressWarnings("serial")
public class Dakijevstina extends JFrame{
Font font = new Font("TimesNew Roman", Font.PLAIN, 12);
public Dakijevstina() {
initComponents();
}
public void initComponents() {
//setting up JFrame
setTitle("Market garden financial software");
setFont(font);
setLayout(new BorderLayout());
setSize(640, 480);
setDefaultCloseOperation(Dakijevstina.EXIT_ON_CLOSE);
setLocationByPlatform(true);
setVisible(true);
//setting up North JPanel
JPanel pnlNorth = new JPanel(new FlowLayout(FlowLayout.LEFT));
pnlNorth.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
add(pnlNorth, BorderLayout.NORTH);
//setting up StatusBar
JPanel pnlSouth = new JPanel(new FlowLayout(FlowLayout.LEFT));
pnlSouth.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
add(pnlSouth, BorderLayout.SOUTH);
//adding buttons to north JPanel
//About
JButton btnAbout = new JButton();
JButton btnExit = new JButton();
try {
Image img = ImageIO.read(getClass().getResource("/images/about.png"));
Image img2 = ImageIO.read(getClass().getResource("/images/exit.png"));
btnAbout.setIcon(new ImageIcon(img));
btnExit.setIcon(new ImageIcon(img2));
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex);
}
btnAbout.setPreferredSize(new Dimension(40, 40));
btnAbout.setMinimumSize(new Dimension(40, 40));
btnAbout.setToolTipText("Information about author");
btnExit.setPreferredSize(new Dimension(40, 40));
btnExit.setMinimumSize(new Dimension(40, 40));
btnExit.setToolTipText("Exit application");
btnExit.addActionListener(e -> exitApp());
pnlNorth.add(btnAbout);
pnlNorth.add(btnExit);
//adding StatusBar to south JPanel
JLabel status = new JLabel("Welcome to Dakijevstina software");
status.setFont(font);
pnlSouth.add(status);
}
//event handlers
public void exitApp() {
this.dispose();
}
这是我大部分时间得到的: 不需要的行为
这就是它的样子: 这就是我想要的
解决方案
推荐阅读
- node.js - uncaughtException - GraphQLError:请求无效
- graphql - GraphQL 无法让查询工作
- python - 如何在python中更改灰度图像中特定类型的像素值?
- c# - Swashbuckle 多态性支持问题
- javascript - 如何在本机反应中解决“无法读取未定义的属性'路由器'”?
- python - python这个函数如何到达第10行
- java - java 编译:源、目标和发布支持的版本
- c# - c# 从服务启动应用程序
- system-verilog - verilog中的映射错误
- python - E1101:模块 'cv2' 没有 'copyMakeBorder' 成员