我在另一个类中有一个JPanel,我希望它在单击“enter”按钮时运行。由于某种原因,它不会这样做,我试图在我的代码中编写一个简单的system.out.println,但是当我想要它运行JPanel时,它不会运行它,有谁知道我的问题是什么?
import inf45.spring2010.examples.gui3.fileChooser;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class clientGUI extends JFrame {
/**
* Variables
*/
private JLabel nameLabel;
private JLabel IPLabel;
private JLabel portLabel;
private JTextField nameText;
private JTextField IPText;
private JTextField portText;
private JButton enterButton;
private JButton cancelButton;
/**
* Constructor for the Frame
*/
public clientGUI () {
JFrame frame = new JFrame ();
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
buildUI();
}
private void buildUI(){
// Setup the GridBagLayout
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
/**
* Set Up the JLabels
*/
nameLabel = new JLabel ("Name:");
getContentPane().add(nameLabel);
layout.setConstraints(
nameLabel,
new GridBagConstraints(
0, 1, 2, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5, 10, 5, 10), 0, 0));
IPLabel = new JLabel ("IP Address:");
getContentPane().add(IPLabel);
layout.setConstraints(
IPLabel,
new GridBagConstraints(
0, 2, 2, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5, 10, 5, 10), 0, 0));
portLabel = new JLabel("Port:");
getContentPane().add(portLabel);
layout.setConstraints(
portLabel,
new GridBagConstraints(
0, 3, 2, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5, 10, 5, 10), 0, 0));
/**
* Setup JTextFields
*/
nameText = new JTextField (15);
getContentPane().add(nameText);
layout.setConstraints(
nameText,
new GridBagConstraints(
2, 1, 2, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5, 10, 5, 10), 0, 0));
IPText = new JTextField (15);
getContentPane().add(IPText);
layout.setConstraints(
IPText,
new GridBagConstraints(
2, 2, 2, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5, 10, 5, 10), 0, 0));
portText = new JTextField(15);
getContentPane().add(portText);
layout.setConstraints(
portText,
new GridBagConstraints(
2, 3, 2, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5, 10, 5, 10), 0, 0));
/**
* Setup JButtons
*/
enterButton = new JButton ("Enter");
getContentPane().add(enterButton);
layout.setConstraints(
enterButton,
new GridBagConstraints(
0, 4, 2, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5, 10, 5, 10), 0, 0));
//Action Listener for enter button that prompts the user to the send GUI
enterButton.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
fileChooser enterFile = new fileChooser();
enterFile.setVisible(true);
enterFile.setSize(150,150);
System.out.println("osama");
}
});
cancelButton = new JButton ("Cancel");
getContentPane().add(cancelButton);
layout.setConstraints(
cancelButton,
new GridBagConstraints(
2, 4, 2, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5, 10, 5, 10), 0, 0));
//ActionListener for the cancel button that should clear all fields.
cancelButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//cancelPressed();
}
});
}
}
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class fileChooser extends JPanel implements ActionListener {
static private final String newline = "\n";
JButton openButton, saveButton;
JTextArea log;
JFileChooser fc;
public fileChooser() {
super(new BorderLayout());
//Create the log first, because the action listeners
//need to refer to it.
log = new JTextArea(5,20);
log.setMargin(new Insets(5,5,5,5));
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);
//Create a file chooser
fc = new JFileChooser();
openButton = new JButton("Select a File...");
openButton.addActionListener(this);
//Create the save button. We use the image from the JLF
//Graphics Repository (but we extracted it from the jar).
saveButton = new JButton("Send Selected File");
saveButton.addActionListener(this);
//For layout purposes, put the buttons in a separate panel
JPanel buttonPanel = new JPanel(); //use FlowLayout
buttonPanel.add(openButton);
// buttonPanel.add(saveButton);
//Add the buttons and the log to this panel.
add(buttonPanel, BorderLayout.PAGE_START);
add(logScrollPane, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
//Handle SEND button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(fileChooser.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Selected: " + file.getName() + "." + newline);
System.out.println("Sent " + file.getName());
} else {
log.append("Open command cancelled by user." + newline);
System.out.println("File selection canceled.");
}
log.setCaretPosition(log.getDocument().getLength());
}
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = fileChooser.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
public static void createAndShowFileChooser() {
//Create and set up the window.
JFrame frame = new JFrame("FileChooserDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frame.add(new fileChooser());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
//Displays and creates the file chooser
createAndShowFileChooser();
}
});
}
}
public class mainClient {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
clientGUI client = new clientGUI();
client.setTitle("Client GUI");
client.setVisible(true);
client.setSize(200,200);
}
}
答案 0 :(得分:3)
JPanel
无法自行显示,必须位于JFrame
内。因此,请fileChooser
扩展JFrame
(并添加所需内容),或将新面板(enterFile)添加到当前帧(根据您的需要)。
答案 1 :(得分:3)
您正在创建新的JPanel,但不是将其添加到顶级容器中。必须将JPanel添加到JFrame,类似于添加标签和文本字段的方式。
另一个选择是让你的filechooser类扩展JDialog,它旨在创建这些类型的Dialogs。您确定不想使用内置的javax.swing.JFileChooser类吗?