基本上有2帧:"StartUpFrame"和"ActivityScreen"StartUpFrame 是使用纯粹的 Netbeans GUI 制作的,而 ActivityScreen 是通过编码制作的,没有任何 Netbeans GUI。
在StartUpFrame上,当我按下"活动列表"按钮时,它应该将框架切换到ActivityScreen,这应该是一个非常简单的操作。
但是,当我自己编译ActivityScreen时,它给了我一些类似于以下内容的警告:
">注意:F:\Java\Lab 8\Lab8.java使用未经检查或不安全的操作。注意:使用 -Xlint:未检查详细信息重新编译。但该文件仍然编译。
当我尝试编译 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
}
活动屏幕.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();
}
}
天.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
你的LinkedList
是字符串。放LinkedList<String>
然后,如果你没有错误地将另一个类对象放入LinkedList
,java 可以检查编译。或者在具有未选中操作的方法之前添加@SuppressWarnings("unchecked")
。