gpt4 book ai didi

java - 为什么我们需要在 swing 应用程序中扩展 JFrame?

转载 作者:搜寻专家 更新时间:2023-10-31 08:08:26 24 4
gpt4 key购买 nike

为什么我们在构建Swing 应用程序时需要扩展JFrame 类。据我所知 extends 用于继承基类。 JFrame 类的所有功能均未在以下程序中使用,但仍对其进行了扩展。我知道我错过了一些信息。是不是好像JFrame类的一些功能在后台运行。

1)代码

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;

public class tuna extends JFrame{

private JTextField item1;
private JTextField item2;
private JTextField item3;
private JPasswordField passwordField;
Container contentPane ;
public tuna(){
super("The title");

setLayout(new FlowLayout());

item1 = new JTextField(10);
contentPane.add(item1);

item2 = new JTextField("enter text here");
add(item2);

item3 = new JTextField("uneditable", 20);
item3.setEditable(false);
add(item3);

passwordField = new JPasswordField("mypass");
add(passwordField);

thehandler handler = new thehandler();

item1.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
passwordField.addActionListener(handler);
}

public static void main(String[] args){
tuna aye = new tuna();
}

private class thehandler implements ActionListener{

public void actionPerformed(ActionEvent event){
String string = "";

if(event.getSource()==item1)
string=String.format("field 1: %s",event.getActionCommand());
else if (event.getSource()==item2)
string=String.format("field 2: %s",event.getActionCommand());
else if (event.getSource()==item3)
string=String.format("field 3: %s",event.getActionCommand());
else if (event.getSource()==passwordField)
string=String.format("password field is: %", event.getActionCommand());
}
}
}

最佳答案

您不需要扩展 JFrame,事实上,我们中进行大量 Swing 编程的许多人都认为扩展此类。我自己尝试扩展我计划改变类的固有行为的类——即覆盖类的非静态方法之一。由于我很少需要为 JFrame 执行此操作,所以我很少想扩展它。

避免扩展它的另一个原因:如果您稍后想在 JDialog 或 JOptionPane 或另一个容器中显示您刚刚创建的 GUI 作为复杂 GUI 的一部分怎么办?如果您的类扩展了 JFrame,这将很难做到。我自己尝试调整我的 GUI 类来创建 JPanel,这样就更容易做到。

基于您的代码的一个愚蠢的例子:

import javax.swing.*;

// this guy extends *nothing*
public class TunaExample {
private static final int COLS = 10;
private JPanel mainPanel = new JPanel(); // this is what I'll add to contentPane
private JTextField field1 = new JTextField(COLS);
private JTextField field2 = new JTextField(COLS);
private JPasswordField passwordField = new JPasswordField(COLS);
private JComponent[] allComponents = { new JLabel("Field 1:"), field1,
new JLabel("Field 2:"), field2, new JLabel("Password:"), passwordField };

public TunaExample() {
field2.setEditable(false);
field2.setFocusable(false);
field1.setText("Field 1");
field2.setText("Uneditable");

for (JComponent comp : allComponents) {
mainPanel.add(comp);
}
}

public JComponent getMainComponent() {
return mainPanel;
}

private static void createAndShowGui() {
TunaExample tunaExample = new TunaExample();

// creating my JFrame only when I need it and where I need it
JFrame frame = new JFrame("Tuna Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(tunaExample.getMainComponent());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

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

关于java - 为什么我们需要在 swing 应用程序中扩展 JFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15867148/

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