基本的に、「StartUpFrame」と「ActivityScreen」の 2 つのフレームがあります。StartUpFrame は純粋にNetbeans GUIを使用して作成されましたが、ActivityScreen はNetbeans GUI を使用せずにコーディングによって作成されました。
StartUpFrame で「List of Activities」ボタンを押すと、フレームが ActivityScreen に切り替わるはずです。これは非常に簡単な操作です。
ただし、ActivityScreen を単独でコンパイルすると、次のような警告が表示されました。
"注: F:\Java\Lab 8\Lab8.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. " しかし、ファイルはまだコンパイルされました。
そして、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