基本上,有两个框架:“StartUpFrame”和“ActivityScreen” StartUpFrame是使用纯粹的 Netbeans GUI 制作的,而ActivityScreen是通过编码而不使用任何Netbeans GUI 制作的。
在StartUpFrame上,当我按下“活动列表”按钮时,它应该将框架切换到ActivityScreen,这应该是一个非常简单的操作。
然而,当我自己编译ActivityScreen时,它给了我一些类似于:
的警告“注意:F:\ Java \ Lab 8 \ Lab8.java使用未经检查或不安全的操作。注意:使用-Xlint重新编译:取消选中以获取详细信息。”, 但该文件仍然编译。
当我尝试编译StartUpFrame时,错误似乎导致按钮操作出现问题,无法正确调用该类
我无法弄清问题是什么。
StartUpFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class StartUpFrame extends JFrame
{
//Creates new form StartUpFrame
public StartUpFrame()
{
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
title = new javax.swing.JLabel();
btnActivities = new javax.swing.JButton();
ButtonExit = new javax.swing.JButton();
btnStudents = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
title.setFont(new java.awt.Font("Arial", 0, 48)); // NOI18N
title.setText("ASA Magement Program");
btnActivities.setFont(new java.awt.Font("Arial", 0, 24)); // NOI18N
btnActivities.setText("List of Activities");
btnActivities.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnActivitiesActionPerformed(evt);
}
});
ButtonExit.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
ButtonExit.setText("Exit");
ButtonExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ButtonExitActionPerformed(evt);
}
});
btnStudents.setFont(new java.awt.Font("Arial", 0, 24)); // NOI18N
btnStudents.setText("List of All Students");
btnStudents.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnStudentsActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(233, Short.MAX_VALUE)
.addComponent(btnActivities)
.addGap(232, 232, 232))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(62, 62, 62)
.addComponent(title))
.addGroup(layout.createSequentialGroup()
.addGap(213, 213, 213)
.addComponent(btnStudents)))
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(258, 258, 258)
.addComponent(ButtonExit, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(title)
.addGap(103, 103, 103)
.addComponent(btnActivities)
.addGap(61, 61, 61)
.addComponent(btnStudents)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 110, Short.MAX_VALUE)
.addComponent(ButtonExit, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>//GEN-END:initComponents
private void ButtonExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonExitActionPerformed
System.exit(0);
}//GEN-LAST:event_ButtonExitActionPerformed
private void btnActivitiesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnActivitiesActionPerformed
ActivityScreen AS = new ActivityScreen();
AS.setVisible(true);
setVisible(false);
}//GEN-LAST:event_btnActivitiesActionPerformed
private void btnStudentsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStudentsActionPerformed
StudentScreen SS = new StudentScreen();
SS.setVisible(true);
setVisible(false);
}//GEN-LAST:event_btnStudentsActionPerformed
//the command line arguments
public static void main(String args[])
{
// Create and display the form
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new StartUpFrame().setVisible(true);
}
});
}
//note to self: NEED TO REMOVE "JAVAX.SWING."
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton ButtonExit;
private javax.swing.JButton btnActivities;
private javax.swing.JButton btnStudents;
private javax.swing.JLabel title;
// End of variables declaration//GEN-END:variables
}
ActivityScreen.java
import java.io.*;
import java.util.*;
import java.util.List;
import javax.swing.JTable;
import javax.swing.*;
import java.awt.*;
import java.awt.Component;
import java.awt.Container;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class MyFrame extends javax.swing.JFrame
{
//Object[][] cell_value = { {"Sunday","Activity1","Activity2","Activity3"}, {"Monday","Activity4","Activity5","Activity6"}, {"Tuesday","Activity7","Activity8",""}};
Object[][] cell_value = new Object[0][5];
//String ColName[] = { "Title1", "Title2", "Title3", "Title4"};
String ColName[] = new String [5];
MyFrame()
{
try{
String s = "";
int n = 0;
int m = 0;
int rowcount = 0;
BufferedReader in = new BufferedReader(new FileReader("Days.dat"));
List list = Collections.synchronizedList(new LinkedList());
while ( (s = in.readLine()) != null ) {
if(!s.equals(""))
{
//System.out.println(s);
list.add(s);
if(s.equals("Sunday")) { ColName[0]=s; }
else if(s.equals("Monday")) { ColName[1]=s; }
else if(s.equals("Tuesday")) { ColName[2]=s; }
else if(s.equals("Wednesday")) { ColName[3]=s; }
else if(s.equals("Thursday")) { ColName[4]=s; }
else {rowcount ++;}
}
}
in.close();
cell_value = new Object[rowcount][5];
for(int i = 0
; i< list.size();i++)
{
if(list.get(i).equals("Sunday"))
{ n=0; m=0; }
else if(list.get(i).equals("Monday"))
{ n=1; m=0; }
else if(list.get(i).equals("Tuesday"))
{ n=2; m=0; }
else if(list.get(i).equals("Wednesday"))
{ n=3; m=0; }
else if(list.get(i).equals("Thursday"))
{ n=4; m=0; }
else
{
cell_value[m][n]=list.get(i);
m++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
Container contentPane = this.getContentPane();
setTitle("Activity Screen");
setSize(1000, 500);
JTable table = new JTable(cell_value, ColName);
JLabel lblTitle = new JLabel("Activities");
lblTitle.setFont(new java.awt.Font("Arial", 1, 24));
JButton btnSave = new JButton("Save");
JButton btnView = new JButton("View");
JButton btnAdd = new JButton("Add");
JButton btnRemove = new JButton("Remove");
JButton btnBack = new JButton("Back");
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
StartUpFrame SUF = new StartUpFrame();
SUF.setVisible(true);
setVisible(false);
}
});
btnView.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
ViewActivity VA = new ViewActivity();
VA.setVisible(true);
}
});
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
AddActivity AA = new AddActivity();
AA.setVisible(true);
}
});
//Lay out the label and scroll pane from top to bottom.
contentPane.add(lblTitle, BorderLayout.PAGE_START);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
table.setAlignmentX(Component.CENTER_ALIGNMENT);
contentPane.add(Box.createRigidArea(new Dimension(0,20)));
contentPane.add(table);
add(new JScrollPane(table));
//Lay out the buttons from left to right.
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
buttonPane.add(Box.createHorizontalGlue());
buttonPane.add(btnSave);
buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
buttonPane.add(btnView);
buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
buttonPane.add(btnAdd);
buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
buttonPane.add(btnRemove);
buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
buttonPane.add(btnBack);
contentPane.add(buttonPane, BorderLayout.PAGE_END);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
table.setCellSelectionEnabled(true);
}
}
class ActivityScreen
{
public static void main(String[] args)
{
new MyFrame();
}
}
days.dat文件
Sunday
Advanced Open Water Dive Certificate
Sailing
Generation Next
Monday
Helping Hands
Beach Touch Rugby
Running Club
Yoga Club
Tennis Lessons
Tuesday
Recycling Club
Best Buddies
Crochet Club
Movie Club
Shooting Club
Wednesday
Table Tennis
Modern Dance
Contemporary Dance
Gavel Club
Thursday
Cooking
Gym Training
答案 0 :(得分:0)
你的LinkedList
是字符串。放LinkedList<String>
然后java可以检查编译,如果你没有在你的LinkedList
中放置另一个类对象时出错。或者使用未经检查的操作在方法之前添加@SuppressWarnings("unchecked")
。