我尝试制作一个程序,保存我的链接和密码或任何可以帮助我的短文,制作此代码。
这是主要课程:
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 2文件,其中保存url作为名称。
这是一个看app的图片:
当按下添加显示2个InputDialog和一个写名称以及我要保存的内容,问题是我添加了一些东西或者我删除了一些内容但是我需要关闭并重新打开程序以查看我的更改make,我尝试用重绘来刷新pan2,但没有。
答案 0 :(得分:2)
您的代码到处都是,例如,您创建了一个删除并添加了什么也没做的侦听器,然后为按钮添加了不同的侦听器,所以我不知道实际上发生了什么... < / p>
此...
if (e.getActionCommand() == "Add") {
不是String
比较在Java中的工作方式。如果您犯了这个简单的错误,那么您可能还没准备好像GUI编程这样复杂的东西
根据我读过的内容,当您添加或删除某个项目时,您需要更新模型,保存它,但不要将任何组件添加到屏幕上。
说了这么多,我建议采用不同的方法并使用JList
或JTable
,for example
答案 1 :(得分:0)
我认为您应首先重新验证,然后在任何更改后重新绘制。重新验证组件是否有任何更改。您也可以在点击它时使用JButton重新加载所有内容。
如果您的代码分开更多,那么建议就更可靠了。一两个类中的所有逻辑都不好。一个简单的程序并不意味着必须放弃良好的编程实践。