- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个按钮,当我单击它时我想打开另一个框架。该按钮之前可以使用,但现在无法打开框架。
第一个程序:
// By: Qamar, Dimitri and Ehsan
// Date: 21/12/2017
// Description: This is the front page of the Address Book
import javax.swing.*;
import java.awt.event.*;
import java.awt. *;
import java.io.*;
public class AddressBook implements ActionListener // Create a new class Address Book
{
JFrame Start=new JFrame("Address Book"); // Set name of Frame
JButton Open; // Create a new button Open
{
Open=new JButton("OPEN"); // set name of button
Start.setSize(500,600); // set size of frame
Start.add(new JLabel(new ImageIcon("backgroundforlab.jpg"))); // add background picture
Start.setVisible(true); // make the frame visible
Start.setLayout(null);
Start.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
Start.getContentPane().add(Open); //add button to frame
Open.setBounds(100,385,295,88); // set size of button
Open.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Start.dispose(); // When button is clicked close frame and open book222
Book c=new Book();
}
public static void main(String ag[])
{
AddressBook A=new AddressBook(); // Run AddressBook
}
}
现在,我要打开的框架:
import javax.swing.*;
import java.awt.event.*;
import java.awt. *;
import java.io.*;
import java.io.FileWriter; // Importing all necessary packages/extensions
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
// Created By: Qamar Irfan, Dimitri Kolechi and Ehsan Saleh
// Date: 21/12/2017
// Description: This is the main program of Address Book. You can Create, Delete, Save and import contacts to this book.
public class Book {
public static void main(String[] args){
// Making the layout of the frame and adding all buttons and labels
JFrame Start=new JFrame("Creating Contacts"); // Create frame
JTable table = new JTable(); // Create Table
FileWriter read; // Ceate new fileWriter
JButton Create;
JButton Quit; // Create new buttons
JButton Delete;
JButton Save;
JButton Import;
JButton Help;
JLabel name1 = new JLabel("Last Name");
JLabel name2= new JLabel ("First Name");
JLabel phone= new JLabel ("Phone Number"); // Create Labels
JLabel address= new JLabel ("Address");
JLabel pcode= new JLabel ("Postal Code");
JLabel eaddress= new JLabel ("Email Address");
JLabel note= new JLabel ("Note");
JTextField lastName = new JTextField();
JTextField firstName= new JTextField();
JTextField phoneNumber= new JTextField();
JTextField addressLocation= new JTextField(); // Create Text Fields
JTextField postalCode= new JTextField();
JTextField emailAddress= new JTextField();
JTextField noteMemo= new JTextField();
{
Create=new JButton("Create Contact");
Quit= new JButton ("Quit"); // set name of buttons
Delete= new JButton ("Delete a Contact");
Save= new JButton ("Save Contacts");
Import= new JButton ("Import Saved Contacts");
Help= new JButton ("Instructions");
Start.setSize(1215,600); // set size of frame
Start.setVisible(true); // make the frame visible
Start.setLayout(null);
Start.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
Start.getContentPane().add(Create);
Start.getContentPane().add(Quit); // add all buttons to frame
Start.getContentPane().add(Delete);
Start.getContentPane().add(Save);
Start.getContentPane().add(Import);
Start.getContentPane().add(Help);
Start.getContentPane().add(name1);
Start.getContentPane().add(name2);
Start.getContentPane().add(phone);
Start.getContentPane().add(address); // add all Labels
Start.getContentPane().add(pcode);
Start.getContentPane().add(eaddress);
Start.getContentPane().add(note);
Start.getContentPane().add(lastName);
Start.getContentPane().add(firstName);
Start.getContentPane().add(phoneNumber);
Start.getContentPane().add(addressLocation); // add all Text Field
Start.getContentPane().add(postalCode);
Start.getContentPane().add(emailAddress);
Start.getContentPane().add(noteMemo);
Help.setBounds(820,150,200,30);
Create.setBounds(700,20,200,30); // set size of buttons
Delete.setBounds(700,90,200,30);
Save.setBounds(950,90,200,30);
Import.setBounds(950,20,200,30);
Quit.setBounds(820,200,200,30);
name1.setBounds(40,11,100,100);
name2.setBounds(250,11,100,100);
phone.setBounds(460,11,100,100); // set location of all Labels
address.setBounds(40,75,100,100);
pcode.setBounds(250,75,100,100);
eaddress.setBounds(460,75,100,100);
note.setBounds(160,150,100,100);
lastName.setBounds(105,50,100,25);
firstName.setBounds(320,50,100,25);
phoneNumber.setBounds(550,50,100,25);
addressLocation.setBounds(105,114,100,25); // set location of all Text Fields
postalCode.setBounds(320,114,100,25);
emailAddress.setBounds(550,114,100,25);
noteMemo.setBounds(200,180,300,50);
// Creating The Table
Object[] columns = {"Last Name","First Name","Phone Number","Address","Postal Code","Email Address","Note"};
DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(columns); // making the table
table.setModel(model);
table.setBackground(Color.LIGHT_GRAY);
table.setForeground(Color.black);
Font font = new Font("",4,12); // setting the table up
table.setFont(font);
table.setRowHeight(30);
JScrollPane pane = new JScrollPane(table);
pane.setBounds(0, 275, 1200, 700); // setting size and location of table
Start.setLayout(null); // adding table to frame
Start.add(pane);
Object[] row = new Object[7];
// Setting the functions of all buttons
Quit.addActionListener(new ActionListener(){
@Override // setting the quit buttons function
public void actionPerformed(ActionEvent e) {
System.exit(0); // When Quit is clicked close frame
}
});
Help.addActionListener(new ActionListener(){
@Override // setting the Help buttons function
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Type the credentials in the fields, make sure to add '/' at end of every field. after typing, click create, make sure everytime create a contact to save it. once saved delete contact and then save another contact.","Instructions", JOptionPane.INFORMATION_MESSAGE);
// When Help is clicked open pop up box with instructions
}
});
Delete.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
int i = table.getSelectedRow(); // setting the delete buttons function
if(i >= 0){
model.removeRow(i); // delete the selected row
}
else{
System.out.println("Please select a Contact"); // If no row is selected than nothing will be deleted
}
}
});
Create.addActionListener(new ActionListener(){
@Override // setting the create buttons function
public void actionPerformed(ActionEvent e) {
row[0] = lastName.getText();
row[1] = firstName.getText();
row[2] = phoneNumber.getText(); // when create is presses get
row[3] = addressLocation.getText(); // the data from the textfields
row[4] = postalCode.getText();
row[5] = emailAddress.getText();
row[6] = noteMemo.getText();
model.addRow(row);
}
});
Save.addActionListener(new ActionListener(){
@Override // setting the Save button function
public void actionPerformed(ActionEvent e){
try{
File file = new File("records.txt");
// if the file records.txt does not exist
// then the program will create one itself
if(!file.exists()){
file.createNewFile();
}
FileWriter read = new FileWriter("records.txt", true); // create new FileWriter
BufferedWriter write1 = new BufferedWriter(read); // create new BufferedWriter
for(int i = 0; i < table.getRowCount(); i++){ // get the amount of rows in JTable
for(int j = 0; j < table.getColumnCount(); j++){
write1.write(table.getModel().getValueAt(i, j)+" "); // write all data from table in records.txt
}
write1.write("\r\n"); // add space between every contact
}
write1.close();
read.close();
JOptionPane.showMessageDialog(null, "All Contacts Saved"); // when contacts successfully transferred show pop up box
}catch(Exception ex){
ex.printStackTrace();
}
}
});
Import.addActionListener(new ActionListener(){
@Override // setting the Import button function
public void actionPerformed(ActionEvent e){
try {
BufferedReader write2 = new BufferedReader(new FileReader("records.txt")); // find the file records.txt
Object[] tableLines = write2.lines().toArray();
for(int i = 0; i < tableLines.length; i++) // add contacts to JTable
{
String line = tableLines[i].toString().trim();
String[] dataRow = line.split("/"); // every space means new column
model.addRow(dataRow);
}
} catch (Exception ex) {
}
}
});
}
}
}
最佳答案
更正您的图书类,使 Book 扩展 JFrame,然后按如下方式重新组织您的代码。
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
// Importing all necessary packages/extensions
// Created By: Qamar Irfan, Dimitri Kolechi and Ehsan Saleh
// Date: 21/12/2017
// Description: This is the main program of Address Book. You can Create, Delete, Save and import contacts to this book.
public class Book extends JFrame {
// Making the layout of the frame and adding all buttons and labels
JTable table = new JTable(); // Create Table
DefaultTableModel model = new DefaultTableModel();
JTextField lastName = new JTextField();
JTextField firstName = new JTextField();
JTextField phoneNumber = new JTextField();
JTextField addressLocation = new JTextField(); // Create Text Fields
JTextField postalCode = new JTextField();
JTextField emailAddress = new JTextField();
JTextField noteMemo = new JTextField();
Object[] row = new Object[7];
public Book() {
super("Creating Contacts"); // Create frame
init();
}
public void init() {
JLabel name1 = new JLabel("Last Name");
JLabel name2 = new JLabel("First Name");
JLabel phone = new JLabel("Phone Number"); // Create Labels
JLabel address = new JLabel("Address");
JLabel pcode = new JLabel("Postal Code");
JLabel eaddress = new JLabel("Email Address");
JLabel note = new JLabel("Note");
JButton Create = new JButton("Create Contact");
JButton Quit = new JButton("Quit"); // set name of buttons
JButton Delete = new JButton("Delete a Contact");
JButton Save = new JButton("Save Contacts");
JButton Import = new JButton("Import Saved Contacts");
JButton Help = new JButton("Instructions");
setSize(1215, 600); // set size of frame
setVisible(true); // make the frame visible
setLayout(null);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
getContentPane().add(Create);
getContentPane().add(Quit); // add all buttons to frame
getContentPane().add(Delete);
getContentPane().add(Save);
getContentPane().add(Import);
getContentPane().add(Help);
getContentPane().add(name1);
getContentPane().add(name2);
getContentPane().add(phone);
getContentPane().add(address); // add all Labels
getContentPane().add(pcode);
getContentPane().add(eaddress);
getContentPane().add(note);
getContentPane().add(lastName);
getContentPane().add(firstName);
getContentPane().add(phoneNumber);
getContentPane().add(addressLocation); // add all Text Field
getContentPane().add(postalCode);
getContentPane().add(emailAddress);
getContentPane().add(noteMemo);
Help.setBounds(820, 150, 200, 30);
Create.setBounds(700, 20, 200, 30); // set size of buttons
Delete.setBounds(700, 90, 200, 30);
Save.setBounds(950, 90, 200, 30);
Import.setBounds(950, 20, 200, 30);
Quit.setBounds(820, 200, 200, 30);
name1.setBounds(40, 11, 100, 100);
name2.setBounds(250, 11, 100, 100);
phone.setBounds(460, 11, 100, 100); // set location of all Labels
address.setBounds(40, 75, 100, 100);
pcode.setBounds(250, 75, 100, 100);
eaddress.setBounds(460, 75, 100, 100);
note.setBounds(160, 150, 100, 100);
lastName.setBounds(105, 50, 100, 25);
firstName.setBounds(320, 50, 100, 25);
phoneNumber.setBounds(550, 50, 100, 25);
addressLocation.setBounds(105, 114, 100, 25); // set location of all
// Text Fields
postalCode.setBounds(320, 114, 100, 25);
emailAddress.setBounds(550, 114, 100, 25);
noteMemo.setBounds(200, 180, 300, 50);
// Creating The Table
Object[] columns = { "Last Name", "First Name", "Phone Number",
"Address", "Postal Code", "Email Address", "Note" };
model.setColumnIdentifiers(columns); // making the table
table.setModel(model);
table.setBackground(Color.LIGHT_GRAY);
table.setForeground(Color.black);
Font font = new Font("", 4, 12); // setting the table up
table.setFont(font);
table.setRowHeight(30);
JScrollPane pane = new JScrollPane(table);
pane.setBounds(0, 275, 1200, 700); // setting size and location of
// table
setLayout(null); // adding table to frame
add(pane);
// Setting the functions of all buttons
Quit.addActionListener(new ActionListener() {
@Override
// setting the quit buttons function
public void actionPerformed(ActionEvent e) {
System.exit(0); // When Quit is clicked close frame
}
});
Help.addActionListener(new ActionListener() {
@Override
// setting the Help buttons function
public void actionPerformed(ActionEvent e) {
JOptionPane
.showMessageDialog(
null,
"Type the credentials in the fields, make sure to add '/' at end of every field. after typing, click create, make sure everytime create a contact to save it. once saved delete contact and then save another contact.",
"Instructions", JOptionPane.INFORMATION_MESSAGE);
// When Help is clicked open pop up box with instructions
}
});
Delete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int i = table.getSelectedRow(); // setting the delete
// buttons function
if (i >= 0) {
model.removeRow(i); // delete the selected row
} else {
System.out.println("Please select a Contact"); // If no
// row
// is
// selected
// than
// nothing
// will
// be
// deleted
}
}
});
Create.addActionListener(new ActionListener() {
@Override
// setting the create buttons function
public void actionPerformed(ActionEvent e) {
row[0] = lastName.getText();
row[1] = firstName.getText();
row[2] = phoneNumber.getText(); // when create is presses
// get
row[3] = addressLocation.getText(); // the data from the
// textfields
row[4] = postalCode.getText();
row[5] = emailAddress.getText();
row[6] = noteMemo.getText();
model.addRow(row);
}
});
Save.addActionListener(new ActionListener() {
@Override
// setting the Save button function
public void actionPerformed(ActionEvent e) {
try {
File file = new File("records.txt");
// if the file records.txt does not exist
// then the program will create one itself
if (!file.exists()) {
file.createNewFile();
}
FileWriter read = new FileWriter("records.txt", true); // create
// new
// FileWriter
BufferedWriter write1 = new BufferedWriter(read); // create
// new
// BufferedWriter
for (int i = 0; i < table.getRowCount(); i++) { // get
// the
// amount
// of
// rows
// in
// JTable
for (int j = 0; j < table.getColumnCount(); j++) {
write1.write(table.getModel().getValueAt(i, j)
+ " "); // write all data from table in
// records.txt
}
write1.write("\r\n"); // add space between every
// contact
}
write1.close();
read.close();
JOptionPane.showMessageDialog(null, "All Contacts Saved"); // when
// contacts
// successfully
// transferred
// show
// pop
// up
// box
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
Import.addActionListener(new ActionListener() {
@Override
// setting the Import button function
public void actionPerformed(ActionEvent e) {
try {
BufferedReader write2 = new BufferedReader(new FileReader(
"records.txt")); // find the file // records.txt
Object[] tableLines = write2.lines().toArray();
for (int i = 0; i < tableLines.length; i++) // add //
// contacts //
// to JTable
{
String line = tableLines[i].toString().trim();
String[] dataRow = line.split("/"); // every space //
// means new //
// column
model.addRow(dataRow);
}
} catch (Exception ex) {
}
}
});
}
}
// END OF PROGRAM
关于java - 无法从按钮打开其他框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48375089/
我通过 spring ioc 编写了一些 Rest 应用程序。但我无法解决这个问题。这是我的异常(exception): org.springframework.beans.factory.BeanC
我对 TestNG、Spring 框架等完全陌生,我正在尝试使用注释 @Value通过 @Configuration 访问配置文件注释。 我在这里想要实现的目标是让控制台从配置文件中写出“hi”,通过
为此工作了几个小时。我完全被难住了。 这是 CS113 的实验室。 如果用户在程序(二进制计算器)结束时选择继续,我们需要使用 goto 语句来到达程序的顶部。 但是,我们还需要释放所有分配的内存。
我正在尝试使用 ffmpeg 库构建一个小的 C 程序。但是我什至无法使用 avformat_open_input() 打开音频文件设置检查错误代码的函数后,我得到以下输出: Error code:
使用 Spring Initializer 创建一个简单的 Spring boot。我只在可用选项下选择 DevTools。 创建项目后,无需对其进行任何更改,即可正常运行程序。 现在,当我尝试在项目
所以我只是在 Mac OS X 中通过 brew 安装了 qt。但是它无法链接它。当我尝试运行 brew link qt 或 brew link --overwrite qt 我得到以下信息: ton
我在提交和 pull 时遇到了问题:在提交的 IDE 中,我看到: warning not all local changes may be shown due to an error: unable
我跑 man gcc | grep "-L" 我明白了 Usage: grep [OPTION]... PATTERN [FILE]... Try `grep --help' for more inf
我有一段代码,旨在接收任何 URL 并将其从网络上撕下来。到目前为止,它运行良好,直到有人给了它这个 URL: http://www.aspensurgical.com/static/images/a
在过去的 5 个小时里,我一直在尝试在我的服务器上设置 WireGuard,但在完成所有设置后,我无法 ping IP 或解析域。 下面是服务器配置 [Interface] Address = 10.
我正在尝试在 GitLab 中 fork 我的一个私有(private)项目,但是当我按下 fork 按钮时,我会收到以下信息: No available namespaces to fork the
我这里遇到了一些问题。我是 node.js 和 Rest API 的新手,但我正在尝试自学。我制作了 REST API,使用 MongoDB 与我的数据库进行通信,我使用 Postman 来测试我的路
下面的代码在控制台中给出以下消息: Uncaught DOMException: Failed to execute 'appendChild' on 'Node': The new child el
我正在尝试调用一个新端点来显示数据,我意识到在上一组有效的数据中,它在数据周围用一对额外的“[]”括号进行控制台,我认为这就是问题是,而新端点不会以我使用数据的方式产生它! 这是 NgFor 失败的原
我正在尝试将我的 Symfony2 应用程序部署到我的 Azure Web 应用程序,但遇到了一些麻烦。 推送到远程时,我在终端中收到以下消息 remote: Updating branch 'mas
Minikube已启动并正在运行,没有任何错误,但是我无法 curl IP。我在这里遵循:https://docs.traefik.io/user-guide/kubernetes/,似乎没有提到关闭
每当我尝试docker组成任何项目时,都会出现以下错误。 我尝试过有和没有sudo 我在这台机器上只有这个问题。我可以在Mac和Amazon WorkSpace上运行相同的容器。 (myslabs)
我正在尝试 pip install stanza 并收到此消息: ERROR: No matching distribution found for torch>=1.3.0 (from stanza
DNS 解析看起来不错,但我无法 ping 我的服务。可能是什么原因? 来自集群中的另一个 Pod: $ ping backend PING backend.default.svc.cluster.l
我正在使用Hibernate 4 + Spring MVC 4当我开始 Apache Tomcat Server 8我收到此错误: Error creating bean with name 'wel
我是一名优秀的程序员,十分优秀!