导入没问题,我只想在点击搜索按钮后在第二个JFrame中打印出JTextArea中的结果。
当我运行此程序时,我的整个JFrame面板显示一个名为“search”的巨大全屏按钮。
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
private static final String DB_PATH = "C:/Users/Abed/Documents/Neo4j/Movies2.graphdb";
public static GraphDatabaseService graphDB;
public static void main(String[] args) throws Exception {
showFrame();
}
public static void showFrame() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(500, 500);
final JTextField actorInput = new JTextField(10);
final JTextField genreInput = new JTextField(10);
frame.getContentPane().add(actorInput);
frame.getContentPane().add(genreInput);
JButton submit = new JButton("Search");
submit.setSize(5, 5);
frame.getContentPane().add(submit);
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
graphDB = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
Transaction tx = graphDB.beginTx();
String actor = actorInput.getText();
String genre = genreInput.getText();
final String query;
query = "MATCH (Movie {genre:'"
+ genre
+ "'})<-[:ACTS_IN]-(Person {name:'"
+ actor
+ "'}) "
+ "RETURN Person.name, Movie.genre, COUNT(Movie.title) AS cnt "
+ "ORDER BY cnt DESC " + "LIMIT 100 ";
try {
JFrame frame2 = new JFrame();
frame2.setVisible(true);
JTextArea ta = new JTextArea();
ta.setLineWrap(true);
frame2.add(ta);
ExecutionEngine engine = new ExecutionEngine(graphDB,
StringLogger.SYSTEM);
ExecutionResult result = engine.execute(query);
System.out.println(result);
String resultArea = result.dumpToString();
ta.setText(resultArea);
tx.success();
} finally {
tx.close();
graphDB.shutdown();
}
}
});
}
}
答案 0 :(得分:4)
问题和建议:
setVisible(true)
向其添加所有组件,这可能会导致GUI的不可视化或不准确的可视化。在添加您最初添加的所有组件后,请致电setVisible(true)
。覆盖GUI的按钮解决方案 - 以智能和愉悦的方式阅读并使用布局管理器来创建智能且令人愉悦的GUI。教程可以在这里找到:
例如:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dialog.ModalityType;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class MyMain extends JPanel {
private JTextField actorInput = new JTextField(10);
private JTextField genreInput = new JTextField(10);
public MyMain() {
// JPanels use FlowLayout by default
add(actorInput);
add(genreInput);
add(new JButton(new SubmitAction("Submit")));
add(new JButton(new ExitAction("Exit", KeyEvent.VK_X)));
}
private class SubmitAction extends AbstractAction {
public SubmitAction(String name) {
super(name);
}
@Override
public void actionPerformed(ActionEvent evt) {
String actor = actorInput.getText();
String genre = genreInput.getText();
// call database code here in a background thread
// show a dialog with information
String textToDisplay = "Text to display\n";
textToDisplay += "Actor: " + actor + "\n";
textToDisplay += "Genre: " + genre + "\n";
InfoDialogPanel dlgPanel = new InfoDialogPanel();
dlgPanel.textAreaSetText(textToDisplay);
Window window = SwingUtilities.getWindowAncestor(MyMain.this);
JDialog modalDialog = new JDialog(window, "Dialog", ModalityType.APPLICATION_MODAL);
modalDialog.getContentPane().add(dlgPanel);
modalDialog.pack();
modalDialog.setLocationByPlatform(true);
modalDialog.setVisible(true);
}
}
private static void createAndShowGui() {
MyMain mainPanel = new MyMain();
JFrame frame = new JFrame("MyMain");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class InfoDialogPanel extends JPanel {
private JTextArea textArea = new JTextArea(10, 20);
private JScrollPane scrollPane = new JScrollPane(textArea);
public InfoDialogPanel() {
JPanel btnPanel = new JPanel();
btnPanel.add(new JButton(new ExitAction("Exit", KeyEvent.VK_X)));
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(btnPanel, BorderLayout.PAGE_END);
}
public void textAreaSetText(String text) {
textArea.setText(text);
}
public void textAreaAppend(String text) {
textArea.append(text);
}
}
class ExitAction extends AbstractAction {
public ExitAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent evt) {
Component component = (Component) evt.getSource();
Window win = SwingUtilities.getWindowAncestor(component);
win.dispose();
}
}