这是我的剧本
<input type="submit" value="Save" name="btnPass" class="button" onclick="myAlert()";/>
,这是我的按钮
package test;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.util.List;
import java.awt.event.*;
import java.io.*;
import java.io.File;
import java.util.*;
public class Testar extends JFrame {
JComboBox place;
JTextField searchField;
JButton searchButton, hideButton, deletePlaceButton, whatIsHere, hideCat, newCat, delCat;
JFileChooser jfc = new JFileChooser(".");
boolean changed = false;
DefaultListModel<PlaceCategory> dataModel = new DefaultListModel<>();
JList<PlaceCategory> categoryList = new JList<PlaceCategory>(dataModel);
// DefaultListModel<PlaceCategory> NewDataModel = new DefaultListModel<>();
Testar(){
super("test");
//FILEMENU TOPP
JMenuBar fileBar = new JMenuBar();
setJMenuBar(fileBar);
JMenu archive = new JMenu("File");
fileBar.add(archive);
JMenuItem open = new JMenuItem("Open");
archive.add(open);
open.addActionListener(new OpenLis());
JMenuItem save = new JMenuItem("Save");
archive.add(save);
save.addActionListener(new SaveLis());
//kategorier ÖST
JPanel east = new JPanel();
add(east, BorderLayout.CENTER);
east.add(new JLabel("Categories"));
categoryList.setVisibleRowCount(3);
categoryList.setFixedCellWidth(50);
east.add(new JScrollPane(categoryList));
categoryList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//ACTIONLISTENER
hideCat = new JButton("Hide category");
east.add(hideCat);
newCat = new JButton("New category");
east.add(newCat);
newCat.addActionListener(new NewCatLis());
delCat = new JButton("Delete category");
east.add(delCat);
BoxLayout eastLayout = new BoxLayout(east, BoxLayout.Y_AXIS);
east.setLayout(eastLayout);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,300);
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);
}
class NewCatLis implements ActionListener{
public void actionPerformed(ActionEvent ave){
String categoryName;
Color color = Color.BLACK;
categoryName = JOptionPane.showInputDialog(Testar.this, "Name on category");
color = JColorChooser.showDialog(Testar.this,"Chooser color", color);
PlaceCategory pc = new PlaceCategory(categoryName, color);
dataModel.addElement(pc);
}
}
class OpenLis implements ActionListener{
public void actionPerformed(ActionEvent ave){
int answer = jfc.showSaveDialog(Testar.this);
if(answer != JFileChooser.APPROVE_OPTION){
return;
}
File file = jfc.getSelectedFile();
String filename = file.getAbsolutePath();
try{
FileInputStream fis = new FileInputStream(filename);
ObjectInputStream ois = new ObjectInputStream(fis);
dataModel = (DefaultListModel)ois.readObject();
System.out.println(dataModel);
// NewDataModel = dataModel;
//
// dataModel.clear();
//
// for(int i=0; i < NewDataModel.size(); i++){
// PlaceCategory pc = NewDataModel.get(i);
// dataModel.addElement(pc);
// System.out.println("addar");
// }
//
// NewDataModel.clear();
ois.close();
fis.close();
pack();
validate();
repaint();
changed = false;
} catch(ClassNotFoundException e){
JOptionPane.showMessageDialog(Testar.this, "Something went wrong... "+e.getMessage());
} catch(FileNotFoundException e){
JOptionPane.showMessageDialog(Testar.this, "Can not open the file...");
} catch(IOException e){
JOptionPane.showMessageDialog(Testar.this, "Something went wrong... "+e.getMessage());
}
}
}
class SaveLis implements ActionListener{
public void actionPerformed(ActionEvent ave){
int answer = jfc.showSaveDialog(Testar.this);
if(answer != JFileChooser.APPROVE_OPTION){
return;
}
File file = jfc.getSelectedFile();
String filename = file.getAbsolutePath();
try{
FileOutputStream fos = new FileOutputStream(filename);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(dataModel);
oos.close();
fos.close();
changed = false;
} catch(FileNotFoundException e){
JOptionPane.showMessageDialog(Testar.this, "Can not open the file...");
} catch(IOException e){
JOptionPane.showMessageDialog(Testar.this, "Something went wrong... "+e.getMessage());
}
}
}
public static void main(String[] args){
new Testar();
}
}
我想知道当&#34;好的&#34;以及如何添加重定向脚本的方法。单击警报按钮。我怎么能在我的脚本上添加它?我怎么能在我的脚本上添加它?谢谢!
答案 0 :(得分:2)
使用confirm
代替alert
Window.confirm()方法显示一个带有可选消息的模态对话框和两个按钮,OK和Cancel。
function myAlert() {
var confirmed = confirm("You have changed your username, you will be logged out in order changes to be applied");
// If OK button is clicked
if (confirmed) {
window.location.href = 'google.com';
}
return false; // To stop form from submitting
}
答案 1 :(得分:1)
使用确认对话框时@Tushar是正确的。以下是他的答案的修改版本,确保工作
function myAlert() {
var confirmed = confirm("You have changed your username, you will be logged out in order changes to be applied");
// If OK button is clicked
if (confirmed == true) {
window.open("path-to-logout script","_self");
}
}