gpt4 book ai didi

java - 将 JLabel 从 Java 中的另一个类放在 JScrollPane 上

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

我正在制作家谱,这是我的问题。我有屏幕 NewFamilyTree.javaNewPerson.java

NewFamilyTree.java:

public class NewFamilyTree {
...
private void initialize() {
frame = new JFrame();
frame.setTitle("New family tree");
frame.setBounds(100, 100, 906, 569);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
...
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);

JScrollPane scrollPane = new JScrollPane();
tabbedPane.addTab("Tree", null, scrollPane, null);

panel_1 = new JPanel();
scrollPane.setViewportView(panel_1);
panel_1.setLayout(new MigLayout("", "[][][][][][][][]", "[][][][][][]"));

NewPerson.java:

public class NewPerson{

...

buttonAdd = new JButton("Add");
buttonAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

String names = textFieldNames.getText();
String dateBirth = textFieldDateOfBirth.getText();
String bio = textAreaBio.getText();

Data newData = new Data(names, dateBirth, bio, fileID);

//code that puts new label on scrollpane from NewFamilyTree.java
}
});
buttonAdd.setBackground(new Color(30, 144, 255));
frame.getContentPane().add(buttonAdd, "cell 2 6,grow");
}

我需要通过按 JScrollPane 上的 Add 按钮,从 class NewPerson 中放置 new JLabel NewFamilytree.java。希望有人可以提供帮助,我搜索了很多但无法帮助自己。

编辑:在@mjr回答之后。

我在 NewFamilyTree 中添加了 public JPanel panel_1;。在执行的 Add 操作中,我添加了:

JLabel lblHomer = new JLabel("Homer");
lblHomer.setIcon(new ImageIcon("C:\\Users\\Tinmar\\Desktop\\HomerSimpson3.gif"));
panel_1.add(lblHomer, "cell 7 5");

没有错误,但是 - 按下添加按钮后什么也没有发生。我还添加了 NewPerson EXTENDS NewFamilyTreeofc

最佳答案

  1. NewPerson不需要从 NewFamilyTree 扩展,它没有向类添加任何功能
  2. 而不是使用 JFrameNewPerson ,考虑使用模态 JDialog反而。请参阅How to Make Dialogs了解更多详情
  3. 限制类之间组件的暴露。没有理由NewFamilyTree应该能够访问 NewPerson 使用的“窗口” 。没有理由NewPerson应该向 NewFamilyTree 添加任何内容
  4. 不要将重量级组件(例如 java.awt.Button )与轻量级组件混合使用,这可能会导致无穷无尽的问题...

你需要改变你思考事物的方式。而不是试图使 NewPerson更新NewFamilyTree的UI ,有NewPerson收集用户的详细信息并将此信息传递回 NewFamilyTree所以它可以使用它...

例如...

JButton newPersonButton = new JButton("New Person");
newPersonButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Data data = NewPerson.createPerson(frame);
if (data != null) {
JLabel lblHomer = new JLabel(data.names);
panel_1.add(lblHomer, "cell 7 5");
panel_1.revalidate();
}
}
});

这基本上使用 static方法createPerson它传回 Data 的实例(或者 null 如果用户取消了操作),其中 NewFamilyTree然后就可以使用了。它将代码解耦,如 NewPerson不依赖 NewFamilyTree 中的任何内容和NewFamilyTree保持控制。这就明确了两个类之间的职责范围,也意味着NewPerson可以从任何地方调用...

createPerson方法看起来像这样...

public static Data createPerson(Component comp) {
NewPerson newPerson = new NewPerson();
Window win = SwingUtilities.getWindowAncestor(comp);
JDialog dialog = null;
if (win instanceof Frame) {
dialog = new JDialog((Frame) win, "New person", true);
} else if (win instanceof Dialog) {
dialog = new JDialog((Dialog) win, "New person", true);
} else {
dialog = new JDialog((Frame) null, "New person", true);
}
newPerson.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source instanceof Component) {
Window win = SwingUtilities.getWindowAncestor((Component) source);
win.dispose();
}
}
});
dialog.add(newPerson);
dialog.setVisible(true);

return newPerson.getData();
}

它基本上创建 JDialog 的实例,向用户显示它并等待 NewPerson类触发 ActionEvent ,它用来处理对话框。然后它询问 NewPerson 的实例对于数据...

因为还有一大堆功能我没有讨论过,所以这里有一个完全可运行的示例......

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import net.miginfocom.swing.MigLayout;

public class NewFamilyTree {

private JFrame frame;
private JPanel panel_1;
private JScrollPane scrollPane;
private JTabbedPane tabbedPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
NewFamilyTree window = new NewFamilyTree();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}

});
}

/**
* Create the application.
*/
public NewFamilyTree() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("New family tree");
frame.getContentPane().setBackground(new Color(135, 206, 250));
frame.setBounds(100, 100, 906, 569);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();
panel.setBackground(new Color(30, 144, 255));
frame.getContentPane().add(panel, BorderLayout.EAST);
panel.setLayout(new MigLayout("", "[]", "[][][][][][][][]"));

JButton newPersonButton = new JButton("New Person");
newPersonButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Data data = NewPerson.createPerson(frame);
if (data != null) {
JLabel lblHomer = new JLabel(data.names);
// lblHomer.setIcon(new ImageIcon("C:\\Users\\Tinmar\\Desktop\\HomerSimpson3.gif"));
panel_1.add(lblHomer, "cell 7 5");
panel_1.revalidate();
}
}
});
panel.add(newPersonButton, "cell 0 5");

JButton btnNewButton_1 = new JButton("New button");
panel.add(btnNewButton_1, "cell 0 6");

tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);

scrollPane = new JScrollPane();
tabbedPane.addTab("Tree", null, scrollPane, null);

panel_1 = new JPanel();
scrollPane.setViewportView(panel_1);
panel_1.setLayout(new MigLayout("", "[][][][][][][][]", "[][][][][][]"));

// JLabel lblHomer = new JLabel("Homer");
// lblHomer.setIcon(new ImageIcon("C:\\Users\\Tinmar\\Desktop\\HomerSimpson3.gif"));
// panel_1.add(lblHomer, "cell 7 5");
JScrollPane scrollPane_1 = new JScrollPane();
tabbedPane.addTab("Info", null, scrollPane_1, null);

frame.repaint();

}
//
// /**
// * Launch the application.
// */
// public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
// NewPerson window = new NewPerson();
// window.frame.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
// }

public static class NewPerson extends JPanel {

private JTextField textFieldNames;
private JButton selectPictureButton;
private JLabel labelDateOfBirth;
private JTextField textFieldDateOfBirth;
private JLabel labelShortBio;
private JTextArea textAreaBio;
private JLabel labelSelectPicture;
private JButton buttonAdd;
private String fileID;

private Data data;

/**
* Create the application.
*/
private NewPerson() {
initialize();
}

public static Data createPerson(Component comp) {
NewPerson newPerson = new NewPerson();
Window win = SwingUtilities.getWindowAncestor(comp);
JDialog dialog = null;
if (win instanceof Frame) {
dialog = new JDialog((Frame) win, "New person", true);
} else if (win instanceof Dialog) {
dialog = new JDialog((Dialog) win, "New person", true);
} else {
dialog = new JDialog((Frame) null, "New person", true);
}
newPerson.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source instanceof Component) {
Window win = SwingUtilities.getWindowAncestor((Component) source);
win.dispose();
}
}
});
dialog.add(newPerson);
dialog.pack();
dialog.setLocationRelativeTo(comp);
dialog.setVisible(true);

return newPerson.getData();
}

public void addActionListener(ActionListener listener) {
listenerList.add(ActionListener.class, listener);
}

protected void fireActionPerformed() {
ActionListener[] listeners = listenerList.getListeners(ActionListener.class);
if (listeners != null && listeners.length > 0) {
ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "created");
for (ActionListener listener : listeners) {
listener.actionPerformed(evt);
}
}
}

public Data getData() {
return data;
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
setBackground(new Color(135, 206, 250));
setLayout(new MigLayout("", "[][][grow]", "[][][][grow][][][]"));

JLabel labelNames = new JLabel("Name and Surname:");
add(labelNames, "cell 1 1,alignx trailing");

textFieldNames = new JTextField();
add(textFieldNames, "cell 2 1,growx");
textFieldNames.setColumns(10);

labelDateOfBirth = new JLabel("Date of birth:");
add(labelDateOfBirth, "cell 1 2,alignx center,aligny center");

textFieldDateOfBirth = new JTextField();
add(textFieldDateOfBirth, "cell 2 2,growx");
textFieldDateOfBirth.setColumns(10);

labelShortBio = new JLabel("Bio:");
add(labelShortBio, "cell 1 3,alignx center,aligny center");

textAreaBio = new JTextArea();
add(textAreaBio, "cell 2 3,grow");

labelSelectPicture = new JLabel("Select picture:");
add(labelSelectPicture, "cell 1 4,alignx center,aligny center");

selectPictureButton = new JButton("...");
selectPictureButton.setBackground(new Color(30, 144, 255));
selectPictureButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

JFileChooser chooser = new JFileChooser(new File(System.getProperty("user.home") + "\\Desktop"));

chooser.setDialogTitle("Select Location");
chooser.setFileSelectionMode(JFileChooser.APPROVE_OPTION);
chooser.setAcceptAllFileFilterUsed(false);

if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
fileID = chooser.getSelectedFile().getPath();
// txtField.setText(fileID);
}

}
});
add(selectPictureButton, "cell 2 4");

buttonAdd = new JButton("Add");
buttonAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

String names = textFieldNames.getText();
String dateBirth = textFieldDateOfBirth.getText();
String bio = textAreaBio.getText();

data = new Data(names, dateBirth, bio, fileID);
fireActionPerformed();
}
});
buttonAdd.setBackground(new Color(30, 144, 255));
add(buttonAdd, "cell 2 6,grow");
}

}

public static class Data {

private final String names;
private final String dateBirth;
private final String bio;
private final String fileID;

private Data(String names, String dateBirth, String bio, String fileID) {
this.names = names;
this.dateBirth = dateBirth;
this.bio = bio;
this.fileID = fileID;
}

}

}
  • 不要依赖static提供跨类的功能。如果你必须从其他类(class)获得一些东西,请将其作为引用。 static不是您的 friend ,您应该小心谨慎地使用它
  • 没有充分的理由,不要公开你的类的字段,依赖interface允许类提供或获取信息。这限制了其他类可以做的事情。
  • 分离和隔离责任
  • 看看How to Use Trees

关于java - 将 JLabel 从 Java 中的另一个类放在 JScrollPane 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25317554/

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