我尝试制作一个程序,在其中保存我的链接和密码或任何可以帮助我的短文字,然后编写此代码。
这是主要的类:
import javax.swing.*;
import java.awt.*;
public static void main(String[] args) {
Second ob = new Second();
ob.setLocation(450,30);
ob.setSize(600, 700);
ob.setVisible(true);
ob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
这是第二节课
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
public class Second extends JFrame {
private int i;
private int numberlink = 200;//numarul maxim de linkuri
private int[] deleteTable = new int[numberlink];
private JPanel pan1;
private JButton[] buton = new JButton[numberlink];
private Box box;
private String namefile = "stocarenume.txt";
private int numberbutton;
private Scanner scaner;
private String array_nume[] = new String[numberlink];
private String array_url[] = new String[numberlink];
private JButton butonadd;
private ActionListener listener, listenerDelete;
private String namefile_url = "stocare_url.txt";
private BufferedWriter file_set;
private JScrollPane scrollpane;
private GridBagConstraints constraints = new GridBagConstraints();
private JButton del = new JButton("Delete");
private JRadioButton[] deleteCeck = new JRadioButton[numberlink];
private JPanel pan2 = new JPanel();
private JButton delete = new JButton("Delete");
Second() {
box = new Box(BoxLayout.Y_AXIS);
pan1 = new JPanel();
pan1.setBackground(Color.gray);
pan1.setLocation(0, 0);
add(pan1, BorderLayout.NORTH);
butonadd = new JButton("Add");
pan1.add(butonadd);
pan1.add(delete);
//am facut o variabil listener in care am suprascris pentru cele numberlink de butoane
// in care am facut sa imi returneze linkul din array_url
listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
for (i = 0; i < numberlink; i++) {
if (array_nume[i] == e.getActionCommand()) {
String myString = array_url[i];
//iti pune in clipboard linkul ,ca si cum ai da copy la un test dar tu de fapt apesi doar pe un buton
StringSelection stringSelection = new StringSelection(myString);
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(stringSelection, null);
}
}
}
}
};
delete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "Delete") {
for (i = 0; i < numberlink; i++) {
if (deleteTable[i] == 1) {
array_url[i] = "xxxxxx";
array_nume[i] = "xxxxxx";
}
}
scriere_fisier();
}
}
});
listenerDelete = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object c = (Object) e.getSource();
for (i = 0; i < numberlink; i++) {
if (c == deleteCeck[i]) {
deleteTable[i] = 1;
}
}
}
};
butonadd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "Add") {
numberbutton++;
String nume;
String url;
nume = JOptionPane.showInputDialog(null, "Introduceti numele ");
url = JOptionPane.showInputDialog(null, "Introduceti url ");
array_nume[numberbutton] = nume;
array_url[numberbutton] = url;
scriere_fisier();
}
}
});
citirefisier();
creerebutoane();
}
void citirefisier() {
try {
scaner = new Scanner(new File(namefile));
i = 0;
while (scaner.hasNext()) {
array_nume[i] = scaner.nextLine();
i++;
}
numberbutton = i - 1;
} catch (Exception e) {
}
try {
scaner = new Scanner(new File(namefile_url));
i = 0;
while (scaner.hasNext()) {
array_url[i] = scaner.nextLine();
i++;
}
} catch (Exception e) {
}
}
void creerebutoane() {
pan2.setLayout(new GridBagLayout());
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(10, 0, 0, 0);
constraints.anchor = GridBagConstraints.CENTER;
for (i = 1; i <= numberbutton; i++) {
buton[i] = new JButton(array_nume[i]);
buton[i].addActionListener(listener);
pan2.add(buton[i], constraints);
constraints.gridy++;
}
constraints.gridx = 2;
constraints.gridy = 0;
constraints.insets = new Insets(10, 50, 0, 0);
for (i = 1; i <= numberbutton; i++) {
deleteCeck[i] = new JRadioButton();
deleteCeck[i].addActionListener(listenerDelete);
pan2.add(deleteCeck[i], constraints);
constraints.gridy++;
}
scrollpane = new JScrollPane(pan2);
Dimension d = new Dimension(500, 200);
scrollpane.getVerticalScrollBar().setUnitIncrement(d.height);
d.height *= 10; // Show at least 10 buttons
scrollpane.getViewport().setPreferredSize(d);
add(scrollpane);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
void scriere_fisier() {
try {
file_set = new BufferedWriter(new FileWriter(namefile));
for (i = 1; i <= numberbutton; i++) {
file_set = new BufferedWriter(new FileWriter(namefile, true));
if (array_nume[i].equals("xxxxxx")) {
} else {
file_set.newLine();
file_set.write(array_nume[i]);
}
file_set.close();
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "nu sa putut scrie in " + namefile);
}
try {
file_set = new BufferedWriter(new FileWriter(namefile_url));
for (i = 1; i <= numberbutton; i++) {
file_set = new BufferedWriter(new FileWriter(namefile_url, true));
if (array_url[i].equals("xxxxxx")) {
} else {
file_set.newLine();
file_set.write(array_url[i]);
}
file_set.close();
}
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "nu sa putut scrie in " + namefile_url);
}
}
}
现在,我尝试解释我的make文件,一个make 2文件,其中将URL保存为名称。
这是应用程序外观的图片:
当按添加向我显示2 InputDialog和一个写名称以及我要保存的内容时,问题是我添加了一些内容或删除了一些内容,但是我需要再次关闭并打开程序才能看到所做的更改,我尝试重新绘制以刷新pan2,但是什么也没有。
最佳答案
您的代码无处不在,例如,您创建一个删除并添加侦听器,该侦听器什么也不做,然后向按钮添加不同的侦听器,因此我不知道实际发生了什么...
这个...
if (e.getActionCommand() == "Add") {
不是
String
比较在Java中的工作方式。如果您犯了一个简单的错误,那么您可能还没有准备好像GUI编程这样复杂的事情根据我的阅读,添加或删除项目时,您会更新模型,保存模型,但实际上并没有在屏幕上添加任何组件。
说了这么多,我建议使用另一种方法,并使用
JList
或JTable
,for example