このスイングを閉じて前のスイング メニューに戻るボタンを作成するのに問題があります。私は次のことを試しました...
btnBack = new JButton("Back");
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainMenu gui = new MainMenu(uname);
gui.setVisible(true);
gui.pack();
gui.setLocationRelativeTo(null);
frame.setVisible(false);
frame.dispose();
}
});
これを試すと、エラーが発生します
ローカル変数 frame は内部クラス内からアクセスされます: final として宣言する必要があります
コードをトリミングして JDBC を削除しました
public class newImageViewer implements ActionListener {
JLabel lblInstru, lblInstruVal, lblInstrumentID, lblImgnameVal, lblImgtagVal, lblImgsizeVal, lblDateVal, lblImgpathVal, lblTakenbyVal, lblNoteVal, lblImgpath, lblTakenby, lblSalary, lblImgsize, lblDate, lblS,
lblSVal, lblNote, lblImgtag, lblImgname, imagel;
JTextField txtDate, txtImgpath, txtTakenby, txtImgname, txtImgtag, txtImgsize, txtNote, txtInstrumentID;
JButton btnAdd, btnUpdate, btnDelete, btnPrev, btnNext, btnBack;
ResultSet rs;
private ImageIcon image1;
String imagepath, uname;
static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mysql";
static final String USERNAME = "root";
static final String PASSWORD = "root";
public static void main(String[] args) {
newImageViewer obj = new newImageViewer();
obj.createUI();
}
private void createUI() {
JFrame frame = new JFrame("Image Details");
JPanel pnlInput = new JPanel(new GridLayout(4, 2));
lblImgname = new JLabel(" Image Name : ");
txtImgname = new JTextField();
lblImgtag = new JLabel(" ImageTag : ");
txtImgtag = new JTextField();
lblDate = new JLabel(" Date Stamp : ");
txtDate = new JTextField(15);
lblImgpath = new JLabel(" Image Path : ");
txtImgpath = new JTextField();
lblTakenby = new JLabel(" Taken By : ");
txtTakenby = new JTextField();
lblImgsize = new JLabel(" Image Size : ");
txtImgsize = new JTextField();
lblNote = new JLabel(" Note : ");
txtNote = new JTextField();
lblInstrumentID = new JLabel(" Instrument ID : ");
txtInstrumentID = new JTextField();
pnlInput.add(lblImgname);
pnlInput.add(txtImgname);
pnlInput.add(lblImgtag);
pnlInput.add(txtImgtag);
pnlInput.add(lblImgsize);
pnlInput.add(txtImgsize);
pnlInput.add(lblNote);
pnlInput.add(txtNote);
pnlInput.add(lblDate);
pnlInput.add(txtDate);
pnlInput.add(lblImgpath);
pnlInput.add(txtImgpath);
pnlInput.add(lblTakenby);
pnlInput.add(txtTakenby);
pnlInput.add(lblNote);
pnlInput.add(txtNote);
pnlInput.add(lblInstrumentID);
pnlInput.add(txtInstrumentID);
JPanel pnlButton = new JPanel(new GridLayout(1, 3));
btnAdd = new JButton("Add");
btnAdd.addActionListener(this);
btnUpdate = new JButton("Update");
btnUpdate.addActionListener(this);
btnDelete = new JButton("Delete");
btnDelete.addActionListener(this);
btnBack = new JButton("Back");
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainMenu gui = new MainMenu(uname);
gui.setVisible(true);
gui.pack();
gui.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
pnlButton.add(btnAdd);
pnlButton.add(btnUpdate);
pnlButton.add(btnBack);
pnlButton.add(btnDelete);
JPanel pnlNavigate = new JPanel(new GridLayout(1, 2));
btnPrev = new JButton(" << ");
btnPrev.setActionCommand("Prev");
btnPrev.addActionListener(this);
btnNext = new JButton(" >> ");
btnNext.setActionCommand("Next");
btnNext.addActionListener(this);
pnlNavigate.add(btnPrev);
pnlNavigate.add(btnNext);
JPanel pnlNavAns = new JPanel(new GridLayout(4, 2));
lblImgname = new JLabel(" Image Name : ");
lblImgnameVal = new JLabel("Val");
lblImgtag = new JLabel(" Image Tag : ");
lblImgtagVal = new JLabel("Val");
lblImgsize = new JLabel(" Image Size : ");
lblImgsizeVal = new JLabel("Val");
lblDate = new JLabel(" Date Stamp : ");
lblDateVal = new JLabel("Val");
lblImgpath = new JLabel(" Image Path : ");
lblImgpathVal = new JLabel("Val");
lblTakenby = new JLabel(" Taken By : ");
lblTakenbyVal = new JLabel("Val");
lblNote = new JLabel(" Note : ");
lblNoteVal = new JLabel("Val");
lblInstru = new JLabel(" Instrument ID : ");
lblInstruVal = new JLabel("Val");
pnlNavAns.add(lblImgname);
pnlNavAns.add(lblImgnameVal);
pnlNavAns.add(lblImgtag);
pnlNavAns.add(lblImgtagVal);
pnlNavAns.add(lblImgsize);
pnlNavAns.add(lblImgsizeVal);
pnlNavAns.add(lblDate);
pnlNavAns.add(lblDateVal);
pnlNavAns.add(lblImgpath);
pnlNavAns.add(lblImgpathVal);
pnlNavAns.add(lblTakenby);
pnlNavAns.add(lblTakenbyVal);
pnlNavAns.add(lblNote);
pnlNavAns.add(lblNoteVal);
pnlNavAns.add(lblInstru);
pnlNavAns.add(lblInstruVal);
final Container cn = frame.getContentPane();
cn.setLayout(new BoxLayout(cn, BoxLayout.Y_AXIS));
frame.add(pnlInput);
frame.add(pnlButton);
frame.add(pnlNavAns);
frame.add(pnlNavigate);
//If this will not be written, the only frame will be closed
// but the application will be active.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent evt) {
String action = evt.getActionCommand();
if (action.equals("Add")) {
addOperation();
} else if (action.equals("Update")) {
updateOperation();
} else if (action.equals("Delete")) {
deleteOperation();
} else if (action.equals("Prev")) {
preNavigation();
} else if (action.equals("Next")) {
nextNavigation();
}
}
private void addOperation()
private void updateOperation()
private void deleteOperation()
private void preNavigation()
private void nextNavigation()
private void populateValue()
}