gpt4 book ai didi

java - 单击按钮时如何显示新的 Jframe

转载 作者:行者123 更新时间:2023-11-30 11:10:30 25 4
gpt4 key购买 nike

我只想从一帧转到另一帧。我列出了一个列表,您可以从中进行选择,然后当您单击按钮确认时它应该显示一个不同的窗口。遗憾的是它没有。

这是我的第一个 Jframe 中的类,所以我的第一个带有确认按钮的窗口:

package view;

import java.awt.Button;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.jgoodies.forms.factories.DefaultComponentFactory;

public class Scherm1pursco extends JFrame {

/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;

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

/**
* Create the frame.
*/
public Scherm1pursco() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JLabel lblSelectCategory = DefaultComponentFactory.getInstance()
.createTitle("Select Category");
lblSelectCategory.setFont(new Font("Tahoma", Font.BOLD, 16));
lblSelectCategory.setBounds(141, 49, 149, 16);
contentPane.add(lblSelectCategory);

ArrayList<String> purposeCategories = new ArrayList<String>();
purposeCategories.add("Behavioral");
purposeCategories.add("Structural");
purposeCategories.add("Creational");
JList list_purpose = new JList(purposeCategories.toArray());
list_purpose
.setToolTipText("Choose at least one categorie of purpose.");
list_purpose.setBounds(50, 121, 121, 63);
contentPane.add(list_purpose);

ArrayList<String> scopeCategories = new ArrayList<String>();
scopeCategories.add("Class");
scopeCategories.add("Object");
JList list_Scope = new JList(scopeCategories.toArray());
list_Scope.setToolTipText("Choose at least one categorie of Scope");
list_Scope.setBounds(244, 121, 121, 63);
contentPane.add(list_Scope);

JLabel lblPurpose = new JLabel("Purpose categories:");
lblPurpose.setBounds(50, 92, 121, 16);
contentPane.add(lblPurpose);

JLabel lblScopCategories = new JLabel("Scope categories:");
lblScopCategories.setBounds(244, 92, 121, 16);
contentPane.add(lblScopCategories);

Button button = new Button("Confirm>>");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (list_Scope.getSelectedValue().equals("Class")
&& (list_purpose.getSelectedValue()
.equals("Structural"))) {
Scherm2FilterdPatterns S2FP = new Scherm2FilterdPatterns();
S2FP.setVisible(true);;
} else {
System.out.println("error");
}
if (list_Scope.getSelectedValue().equals("Class")
&& (list_purpose.getSelectedValue()
.equals("Behavioral"))) {
Scherm2FilterdPatterns S2FP = new Scherm2FilterdPatterns();
S2FP.setVisible(true);
} else {
System.out.println("error");
}
if (list_Scope.getSelectedValue().equals("Class")
&& (list_purpose.getSelectedValue()
.equals("Creational"))) {
Scherm2FilterdPatterns S2FP = new Scherm2FilterdPatterns();
S2FP.setVisible(true);
} else {
System.out.println("error");
}
if (list_Scope.getSelectedValue().equals("Object")
&& (list_purpose.getSelectedValue()
.equals("Structural"))) {
Scherm2FilterdPatterns S2FP = new Scherm2FilterdPatterns();
S2FP.setVisible(true);
} else {
System.out.println("error");
}
if (list_Scope.getSelectedValue().equals("Object")
&& (list_purpose.getSelectedValue()
.equals("Creational"))) {
Scherm2FilterdPatterns S2FP = new Scherm2FilterdPatterns();
S2FP.setVisible(true);
} else {
System.out.println("error");
}
if (list_Scope.getSelectedValue().equals("Class")
&& (list_purpose.getSelectedValue()
.equals("Behavioral"))) {
Scherm2FilterdPatterns S2FP = new Scherm2FilterdPatterns();
S2FP.setVisible(true);
} else {
System.out.println("error");
}
if (list_Scope.getSelectedValue().equals(null)
&& (list_purpose.getSelectedValue()
.equals(null))) {
System.out.println("error");
}
}
});
button.setBounds(169, 203, 79, 24);
contentPane.add(button);
}
}

这是我的第二个 Jframe,这是应该引用它的类:

     package view;

import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableRowSorter;

public class Scherm2FilterdPatterns extends JFrame {

/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTable table;
private boolean DEBUG = false;

private JTextField filterText;
private JTextField statusText;
private TableRowSorter<MyTableModel> sorter;

/**
* Launch the application.
*/
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}

/**
* Create the frame.
*/
public Scherm2FilterdPatterns() {

super();
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

// Create a table with a sorter.
MyTableModel model = new MyTableModel();
sorter = new TableRowSorter<MyTableModel>(model);
table = new JTable(model);
table.setRowSorter(sorter);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);

// For the purposes of this example, better to have a single
// selection.
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

// When selection changes, provide user with row numbers for
// both view and model.
table.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
int viewRow = table.getSelectedRow();
if (viewRow == 0) {
// Selection got filtered away.
statusText.setText("Diagram is");
} else {
System.out.print("THIS IS THE SOLUTION");
if (viewRow == 1) {
statusText.setText("diagram2 is");
} else {
int modelRow = table
.convertRowIndexToModel(viewRow);
statusText.setText(String.format("poep",
viewRow, modelRow));
if (viewRow == 2) {
statusText.setText("diagram3 is");
} else {
System.out.println("THIS IS THE SOLUTION");
}
}
}
}
});

// Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);

// Add the scroll pane to this panel.
add(scrollPane);

// Create a separate form for filterText and statusText
JPanel form = new JPanel(new SpringLayout());
JLabel l1 = new JLabel("Filter on problem or consequences:",
SwingConstants.TRAILING);
form.add(l1);
filterText = new JTextField();
// Whenever filterText changes, invoke newFilter.
filterText.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
newFilter();
}

public void insertUpdate(DocumentEvent e) {
newFilter();
}

public void removeUpdate(DocumentEvent e) {
newFilter();
}
});
l1.setLabelFor(filterText);
form.add(filterText);
JLabel l2 = new JLabel("Select pattern for more info:",
SwingConstants.TRAILING);
form.add(l2);
statusText = new JTextField();
l2.setLabelFor(statusText);
form.add(statusText);
SpringUtilities.makeCompactGrid(form, 2, 2, 6, 6, 6, 6);
add(form);
}

/**
* Update the row filter regular expression from the expression in the text
* box.
*/

// filter on problem and consequences
private void newFilter() {
RowFilter<MyTableModel, Object> rf = null;
// If current expression doesn't parse, don't update.
try {
rf = RowFilter.regexFilter(filterText.getText(), 1, 2);
} catch (java.util.regex.PatternSyntaxException e) {
return;
}
sorter.setRowFilter(rf);
}

class MyTableModel extends AbstractTableModel {
private String[] columnNames = { "Pattern Name:", "Problem",
"Consequences", };

private Object[][] data = { { "Interpreter", "Smith", "Snowboarding" },
{ "Template Method", "Doe", "Rowing" },
{ "Chain of Responsibility", "Black", "Knitting", },
{ "Command", "White", "Speed reading", },
{ "Iterator", "Brown", "Pool" },
{ "Mediator", "Brown", "Pool", },
{ "Memento", "Brown", "Pool", },
{ "Observer", "Brown", "Pool", },
{ "State", "Brown", "Pool", },
{ "Strategy", "Brown", "Pool", },
{ "Visitor", "Brown", "Pool", }, };

public int getColumnCount() {
return columnNames.length;
}

public int getRowCount() {
return data.length;
}

public String getColumnName(int col) {
return columnNames[col];
}

public Object getValueAt(int row, int col) {
return data[row][col];
}

/*
* JTable uses this method to determine the default renderer/ editor for
* each cell. If we didn't implement this method, then the last column
* would contain text ("true"/"false"), rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}

/*
* Don't need to implement this method unless your table's editable.
*/
public boolean isCellEditable(int row, int col) {
// Note that the data/cell address is constant,
// no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}

/*
* Don't need to implement this method unless your table's data can
* change.
*/
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value + " (an instance of "
+ value.getClass() + ")");
}

data[row][col] = value;
fireTableCellUpdated(row, col);

if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}

private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();

for (int i = 0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j = 0; j < numCols; j++) {
System.out.print(" " + data[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}

/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("Filterd Patterns");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create and set up the content pane.
TableFilterDemo newContentPane = new TableFilterDemo();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);

// Display the window.
frame.pack();
frame.setVisible(true);
}

}

我真的卡在这里了,请帮忙!谢谢你的时间:)对不起我的英语

最佳答案

您的代码似乎过于宽泛且没有必要进行演示,而所需的最少代码却非常小。

即便如此,我也会针对您的问题写一个简短的答案。考虑到您必须退出 jframe1 并移动到 jframe2,在确认按钮的操作执行事件中,只需添加以下行:-

public static void jButton1 ActionPerformed......{
jFrame1.dispose();
jFrame2.setVisible(true);
}

关于java - 单击按钮时如何显示新的 Jframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27668482/

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