Java 通过单击按钮从另一个打开jFrame
我知道这已经被浏览过很多次了,我已经在谷歌上搜索了几个小时,但由于某些原因,我无法让它工作。请对我放松点…我是java的noob,我是一个vb.net男孩。 谢谢你的帮助 这是我调用页面中的按钮侦听器代码。我犯了一个错误Java 通过单击按钮从另一个打开jFrame,java,swing,jpanel,visibility,Java,Swing,Jpanel,Visibility,我知道这已经被浏览过很多次了,我已经在谷歌上搜索了几个小时,但由于某些原因,我无法让它工作。请对我放松点…我是java的noob,我是一个vb.net男孩。 谢谢你的帮助 这是我调用页面中的按钮侦听器代码。我犯了一个错误 Method setvisable(boolean) is unidentified for the type Pay_Types 我也试过这个听众。这里也有同样的错误 private class BtnEditPayMouseListener extends MouseA
Method setvisable(boolean) is unidentified for the type Pay_Types
我也试过这个听众。这里也有同样的错误
private class BtnEditPayMouseListener extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
Pay_Types p = new Pay_Types();
p.setvisable(true);
}
}
这是我的Pay_Types.java代码
package com.dooleyfam.bep1;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JCheckBox;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
public class Pay_Types {
private JFrame frame;
private JCheckBox chckbxEmpty;
private JCheckBox chckbxPerLoadedMile;
private JCheckBox chckbxHourly;
private JCheckBox chckbxOvertime;
private JPanel panel;
private JPanel panel_1;
private JPanel panel_2;
private JCheckBox chckbxPerMile;
private JCheckBox chckbxPerDay;
private JCheckBox chckbxOther;
private JPanel panel_3;
private JCheckBox chckbxPerCwthundred;
private JCheckBox chckbxPercentageOf;
private JCheckBox chckbxOther_1;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_6;
private JTextField textField_7;
private JTextField textField_8;
private JTextField textField_9;
private JLabel lblDenotesThat;
private JButton btnCancel;
private JButton btnOk;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Pay_Types window = new Pay_Types();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Pay_Types() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 503, 285);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
panel = new JPanel();
panel.setBorder(new TitledBorder(null, "Per Mile", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel.setBounds(6, 11, 166, 97);
frame.getContentPane().add(panel);
panel.setLayout(null);
chckbxEmpty = new JCheckBox("Empty");
chckbxEmpty.setBounds(102, 15, 55, 23);
panel.add(chckbxEmpty);
chckbxPerLoadedMile = new JCheckBox("Loaded");
chckbxPerLoadedMile.setBounds(102, 41, 61, 23);
panel.add(chckbxPerLoadedMile);
chckbxOther = new JCheckBox("Other*");
chckbxOther.setBounds(102, 67, 61, 23);
panel.add(chckbxOther);
textField_3 = new JTextField();
textField_3.setBounds(10, 16, 86, 20);
panel.add(textField_3);
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setBounds(10, 42, 86, 20);
panel.add(textField_4);
textField_4.setColumns(10);
panel_1 = new JPanel();
panel_1.setBorder(new TitledBorder(null, "Hourly", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_1.setBounds(182, 11, 297, 71);
frame.getContentPane().add(panel_1);
panel_1.setLayout(null);
chckbxOvertime = new JCheckBox("Overtime");
chckbxOvertime.setBounds(102, 40, 86, 23);
panel_1.add(chckbxOvertime);
chckbxHourly = new JCheckBox("Regular");
chckbxHourly.setBounds(102, 14, 68, 23);
panel_1.add(chckbxHourly);
textField_6 = new JTextField();
textField_6.setBounds(10, 15, 86, 20);
panel_1.add(textField_6);
textField_6.setColumns(10);
textField_7 = new JTextField();
textField_7.setBounds(10, 41, 86, 20);
panel_1.add(textField_7);
textField_7.setColumns(10);
panel_2 = new JPanel();
panel_2.setBorder(new TitledBorder(null, "Per Diem", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_2.setBounds(0, 108, 172, 71);
frame.getContentPane().add(panel_2);
panel_2.setLayout(null);
chckbxPerMile = new JCheckBox("Per Mile");
chckbxPerMile.setBounds(102, 14, 63, 23);
panel_2.add(chckbxPerMile);
chckbxPerDay = new JCheckBox("Per Day");
chckbxPerDay.setBounds(102, 40, 63, 23);
panel_2.add(chckbxPerDay);
textField_8 = new JTextField();
textField_8.setBounds(10, 15, 86, 20);
panel_2.add(textField_8);
textField_8.setColumns(10);
textField_9 = new JTextField();
textField_9.setBounds(10, 42, 86, 20);
panel_2.add(textField_9);
textField_9.setColumns(10);
panel_3 = new JPanel();
panel_3.setBorder(new TitledBorder(null, "Other", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_3.setBounds(182, 82, 297, 97);
frame.getContentPane().add(panel_3);
panel_3.setLayout(null);
chckbxPerCwthundred = new JCheckBox("Per Hundred Weight (CWT)");
chckbxPerCwthundred.setBounds(102, 14, 157, 23);
panel_3.add(chckbxPerCwthundred);
chckbxPercentageOf = new JCheckBox("Percentage of Line Haul (PLH)");
chckbxPercentageOf.setBounds(102, 41, 169, 23);
panel_3.add(chckbxPercentageOf);
chckbxOther_1 = new JCheckBox("Other (Flat Rate, Percentage) *");
chckbxOther_1.setBounds(102, 68, 189, 23);
panel_3.add(chckbxOther_1);
textField = new JTextField();
textField.setBounds(10, 15, 86, 20);
panel_3.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(10, 42, 86, 20);
panel_3.add(textField_1);
textField_1.setColumns(10);
lblDenotesThat = new JLabel("* Denotes that the amount is entered on the main screen.");
lblDenotesThat.setBounds(85, 190, 283, 14);
frame.getContentPane().add(lblDenotesThat);
btnCancel = new JButton("Cancel");
btnCancel.setBounds(390, 215, 89, 23);
frame.getContentPane().add(btnCancel);
btnOk = new JButton("OK");
btnOk.setBounds(291, 215, 89, 23);
frame.getContentPane().add(btnOk);
}
}
再次感谢你的帮助
Thx Andrew我确实忘记了这个问题……我做错了什么?要么制作
Pay\u类型扩展JFrame
要么p.getFrame().setVisible(true)代码>
支付类型不是框架类型,因此您会遇到上述错误。1)请参见2)您的问题是什么?并确保在其末尾添加一个“?”。@mKorbel这应该是一个答案。OP-“魔法编码”永远不起作用。您需要查阅文档。无关:不要对组件进行任何手动大小调整/定位,这是LayoutManager@Andrew汤普森好的,我知道你的坏习惯,看看我现在在这个问题上错在哪里。但是,你发布的链接确实给我留下了一个问题……如果可以的话……我的呼叫页面将是我的主窗口,付费类型更像是一个输入和编辑数据的向导。我希望它能在一个新窗口中打开,任务栏中没有额外的图标。你的建议是什么?你的回答确实帮助我指出了我的错误方向……当我创建Pay_类型时,我没有将其创建为jFrame,而是作为应用程序窗口。但是,你的代码不起作用,我在getFrame()中遇到了与setVisible()相同的错误。@mndooley我们这里没有调试服务,这个答案解决了关于这个问题的异常…你需要在你的类中提供getter方法。环顾四周后,我明白你的意思了…你的权利。Thx@mKorbel我明白了,请理解我仍在学习,正如在原来的帖子中所说的。没必要粗鲁。
package com.dooleyfam.bep1;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JCheckBox;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
public class Pay_Types {
private JFrame frame;
private JCheckBox chckbxEmpty;
private JCheckBox chckbxPerLoadedMile;
private JCheckBox chckbxHourly;
private JCheckBox chckbxOvertime;
private JPanel panel;
private JPanel panel_1;
private JPanel panel_2;
private JCheckBox chckbxPerMile;
private JCheckBox chckbxPerDay;
private JCheckBox chckbxOther;
private JPanel panel_3;
private JCheckBox chckbxPerCwthundred;
private JCheckBox chckbxPercentageOf;
private JCheckBox chckbxOther_1;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_6;
private JTextField textField_7;
private JTextField textField_8;
private JTextField textField_9;
private JLabel lblDenotesThat;
private JButton btnCancel;
private JButton btnOk;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Pay_Types window = new Pay_Types();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Pay_Types() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 503, 285);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
panel = new JPanel();
panel.setBorder(new TitledBorder(null, "Per Mile", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel.setBounds(6, 11, 166, 97);
frame.getContentPane().add(panel);
panel.setLayout(null);
chckbxEmpty = new JCheckBox("Empty");
chckbxEmpty.setBounds(102, 15, 55, 23);
panel.add(chckbxEmpty);
chckbxPerLoadedMile = new JCheckBox("Loaded");
chckbxPerLoadedMile.setBounds(102, 41, 61, 23);
panel.add(chckbxPerLoadedMile);
chckbxOther = new JCheckBox("Other*");
chckbxOther.setBounds(102, 67, 61, 23);
panel.add(chckbxOther);
textField_3 = new JTextField();
textField_3.setBounds(10, 16, 86, 20);
panel.add(textField_3);
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setBounds(10, 42, 86, 20);
panel.add(textField_4);
textField_4.setColumns(10);
panel_1 = new JPanel();
panel_1.setBorder(new TitledBorder(null, "Hourly", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_1.setBounds(182, 11, 297, 71);
frame.getContentPane().add(panel_1);
panel_1.setLayout(null);
chckbxOvertime = new JCheckBox("Overtime");
chckbxOvertime.setBounds(102, 40, 86, 23);
panel_1.add(chckbxOvertime);
chckbxHourly = new JCheckBox("Regular");
chckbxHourly.setBounds(102, 14, 68, 23);
panel_1.add(chckbxHourly);
textField_6 = new JTextField();
textField_6.setBounds(10, 15, 86, 20);
panel_1.add(textField_6);
textField_6.setColumns(10);
textField_7 = new JTextField();
textField_7.setBounds(10, 41, 86, 20);
panel_1.add(textField_7);
textField_7.setColumns(10);
panel_2 = new JPanel();
panel_2.setBorder(new TitledBorder(null, "Per Diem", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_2.setBounds(0, 108, 172, 71);
frame.getContentPane().add(panel_2);
panel_2.setLayout(null);
chckbxPerMile = new JCheckBox("Per Mile");
chckbxPerMile.setBounds(102, 14, 63, 23);
panel_2.add(chckbxPerMile);
chckbxPerDay = new JCheckBox("Per Day");
chckbxPerDay.setBounds(102, 40, 63, 23);
panel_2.add(chckbxPerDay);
textField_8 = new JTextField();
textField_8.setBounds(10, 15, 86, 20);
panel_2.add(textField_8);
textField_8.setColumns(10);
textField_9 = new JTextField();
textField_9.setBounds(10, 42, 86, 20);
panel_2.add(textField_9);
textField_9.setColumns(10);
panel_3 = new JPanel();
panel_3.setBorder(new TitledBorder(null, "Other", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_3.setBounds(182, 82, 297, 97);
frame.getContentPane().add(panel_3);
panel_3.setLayout(null);
chckbxPerCwthundred = new JCheckBox("Per Hundred Weight (CWT)");
chckbxPerCwthundred.setBounds(102, 14, 157, 23);
panel_3.add(chckbxPerCwthundred);
chckbxPercentageOf = new JCheckBox("Percentage of Line Haul (PLH)");
chckbxPercentageOf.setBounds(102, 41, 169, 23);
panel_3.add(chckbxPercentageOf);
chckbxOther_1 = new JCheckBox("Other (Flat Rate, Percentage) *");
chckbxOther_1.setBounds(102, 68, 189, 23);
panel_3.add(chckbxOther_1);
textField = new JTextField();
textField.setBounds(10, 15, 86, 20);
panel_3.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(10, 42, 86, 20);
panel_3.add(textField_1);
textField_1.setColumns(10);
lblDenotesThat = new JLabel("* Denotes that the amount is entered on the main screen.");
lblDenotesThat.setBounds(85, 190, 283, 14);
frame.getContentPane().add(lblDenotesThat);
btnCancel = new JButton("Cancel");
btnCancel.setBounds(390, 215, 89, 23);
frame.getContentPane().add(btnCancel);
btnOk = new JButton("OK");
btnOk.setBounds(291, 215, 89, 23);
frame.getContentPane().add(btnOk);
}
}