gpt4 book ai didi

java - 为什么这段代码注释可以作为JFrame运行?

转载 作者:行者123 更新时间:2023-12-01 17:18:25 25 4
gpt4 key购买 nike

我正在尝试使小程序作为 JFrame 运行。我下面的代码很简单,但应该可以工作。它将作为 JApplet 运行,但是当我转到 RUN AS --> 时,什么也没有出现。

导入java.awt.*;导入java.applet.Applet;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class LifeCycle extends Applet
{

private static final long serialVersionUID = 1L;
String output = "test";
String event;

public void init()
{
gui(); //I am not certain if this needs to be there.
event = "\nInitializing...";
printOutput();
}

public void start()
{
event = "\nStarting...";
printOutput();
}

public void stop()
{
event = "\nStopping...";
printOutput();
}

public void destroy()
{
event = "\nDestroying...";
printOutput();
}

private void printOutput()
{
System.out.println(event);
output += event;
repaint();
}

private void gui() {
JFrame f = new JFrame("Not resizable");
JPanel d = new JPanel();
// LifeCycle a = new LifeCycle();
// a.init();//not working
d.setLayout(new BorderLayout());
d.add(new JButton("a"));
d.add(new JButton("b"));
d.setBackground(Color.RED);
//f.add(new LifeCycle());
f.add(d);
f.setSize(545,340);
f.setResizable(false);
f.setLocationRelativeTo(null);
f.setTitle("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//a.destroy();
}

public void paint(Graphics g)
{
System.out.println("Graphics Paint Method!");
g.drawString(output, 100, 100);
}

public static void main(String[] args) {
LifeCycle l = new LifeCycle();
l.gui();
}
}

我想查看应该更改的代码,但我似乎无法找到为什么这不起作用。我已将按钮添加到要显示的面板中。

最佳答案

  • 不要将 AWT(Applet)与 Swing 组件混合使用。坚持只使用 Swing。
  • 让您的类(class)致力于创建 JPanel。然后,如果您需要小程序,则可以将其放入 JApplet 中;如果需要 JFrame,则可以将其放入 JFrame 中。
  • 阅读 BorderLayout 的使用 - 您将多个组件添加到默认的 BorderLayout.CENTER 位置,并且只有一个组件(最后添加的一个)会显示。

例如...

LifeCycle2.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;

class LifeCycle2 {
private static final int GAP = 5;
private static final int PREF_W = 545;
private static final int PREF_H = 340;
private JPanel mainPanel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return LifeCycle2.this.getPreferredSize();
}
};

public LifeCycle2() {
JPanel buttonPanel = new JPanel(new GridLayout(1, 0, GAP, 0));
buttonPanel.add(new JButton("A"));
buttonPanel.add(new JButton("B"));
buttonPanel.setOpaque(false);
JPanel flowLayoutPanel = new JPanel();
flowLayoutPanel.setOpaque(false);
flowLayoutPanel.add(buttonPanel);

mainPanel.setLayout(new BorderLayout());
mainPanel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
mainPanel.setBackground(Color.red);
mainPanel.add(flowLayoutPanel, BorderLayout.NORTH);
}

public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}

public JComponent getMainPanel() {
return mainPanel;
}
}

显示为 JFrame,LifeCycleFrame.java

import javax.swing.*;

public class LifeCycleFrame {

private static void createAndShowGui() {
LifeCycle2 lifeCycle2 = new LifeCycle2();

JFrame frame = new JFrame("LifeCycleTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(lifeCycle2.getMainPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

显示为小程序,LifeCycleApplet.java

import java.lang.reflect.InvocationTargetException;    
import javax.swing.JApplet;
import javax.swing.SwingUtilities;

public class LifeCycleApplet extends JApplet {
@Override
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
LifeCycle2 lifeCycle2 = new LifeCycle2();
getContentPane().add(lifeCycle2.getMainPanel());
}
});
} catch (InvocationTargetException | InterruptedException e) {
e.printStackTrace();
}
}
}

关于java - 为什么这段代码注释可以作为JFrame运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20456774/

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