gpt4 book ai didi

java - 当我尝试创建 JFrame 时没有任何显示。 (JButtons、JMenuBar 等)

转载 作者:行者123 更新时间:2023-12-02 06:34:28 25 4
gpt4 key购买 nike

我正在开发一个使用 GUI 的探路者程序。在添加一些东西之前,我尝试运行该程序,它显示一切正常。但在对其进行更多处理后,它不再显示按钮和菜单栏。

这是代码。一些变量名也是瑞典语的,但我希望这不会成为问题。 (请记住,该计划远未完成。)提前致谢!

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;

public class Pathfinder extends JFrame {

JButton hittaVäg, visaFörbindelse, nyPlats, nyFörbindelse, ändraFörbindelse;
JMenuBar menyBar;
JMenuItem ny, avsluta, hittaVägMeny, visaFörbindelseMeny, nyPlatsMeny, nyFörbindelseMeny, ändraFörbindelseMeny;
String str = System.getProperty("user.dir");
JFileChooser jfc;
BildPanel Bild = null;

Pathfinder(){

super("PathFinder");
setLayout(new BorderLayout());
setSize(590, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
jfc = new JFileChooser(".");

JPanel norra = new JPanel();
add(norra, "norra");


JButton hittaVäg = new JButton("Hitta väg");
JButton visaFörbindelse = new JButton("Visa förbindelse");
JButton nyPlats = new JButton("Ny plats");
JButton nyFörbindelse = new JButton("Ny förbindelse");
JButton ändraFörbindelse = new JButton("Ändra förbindelse");

norra.add(hittaVäg);
norra.add(visaFörbindelse);
norra.add(nyPlats);
norra.add(nyFörbindelse);
norra.add(ändraFörbindelse);

hittaVäg.addActionListener(new HittaLyss());
visaFörbindelse.addActionListener(new VisaLyss());
nyPlats.addActionListener(new NyPlatsLyss());
nyFörbindelse.addActionListener(new NyFörbindelseLyss());
ändraFörbindelse.addActionListener(new NyFörbindelseLyss());


JMenuBar menyBar = new JMenuBar();
setJMenuBar(menyBar);

JMenu arkivMeny = new JMenu("Arkiv");
JMenu operationerMeny = new JMenu("Operationer");

menyBar.add(arkivMeny);
menyBar.add(operationerMeny);


JMenuItem ny = new JMenuItem("Ny");
JMenuItem avsluta = new JMenuItem("Avsluta");

arkivMeny.add(ny);
arkivMeny.add(avsluta);

ny.addActionListener(new NyLyss());
avsluta.addActionListener(new AvslutaLyss());


JMenuItem hittaVägMeny = new JMenuItem("Hitta väg");
JMenuItem visaFörbindelseMeny = new JMenuItem("Visa förbindelse");


JMenuItem nyPlatsMeny = new JMenuItem("Ny plats");
JMenuItem nyFörbindelseMeny = new JMenuItem("Ny förbindelse");
JMenuItem ändraFörbindelseMeny = new JMenuItem("Ändra förbindelse");

operationerMeny.add(hittaVägMeny);
operationerMeny.add(visaFörbindelseMeny);
operationerMeny.add(nyPlatsMeny);
operationerMeny.add(nyFörbindelseMeny);
operationerMeny.add(ändraFörbindelseMeny);

hittaVäg.addActionListener(new HittaLyss());
visaFörbindelse.addActionListener(new VisaLyss());
nyPlats.addActionListener(new NyPlatsLyss());
nyFörbindelse.addActionListener(new NyFörbindelseLyss());
ändraFörbindelse.addActionListener(new ÄndraFörbindelseLyss());




}

class HittaLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){

}

}
class VisaLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){

}

}
class NyPlatsLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){

}

}
class NyFörbindelseLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){

}

}
class ÄndraFörbindelseLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){

}

}
class NyLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
int svar = jfc.showOpenDialog(Pathfinder.this);
if (svar == JFileChooser.APPROVE_OPTION){
File f = jfc.getSelectedFile();
String filnamn = f.getAbsolutePath();
if (Bild != null)
remove(Bild);
Bild = new BildPanel(filnamn);
add(Bild, BorderLayout.CENTER);
validate();
repaint();
pack();
}
}

}
class AvslutaLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){

}

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

最佳答案

问题之一是 IllegalArgumentException

add(norra, "norra");

框架内容 Pane 的布局设置为 BorderLayout,但此布局不理解 "norra" 约束。参见 How to Use BorderLayout有关更多详细信息和示例。

此外,您应该在添加并初始化所有组件后调用setVisible

关于java - 当我尝试创建 JFrame 时没有任何显示。 (JButtons、JMenuBar 等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23303363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com