package?sleest;
主要從事網(wǎng)頁設(shè)計、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、成都響應(yīng)式網(wǎng)站建設(shè)公司、程序開發(fā)、微網(wǎng)站、微信平臺小程序開發(fā)等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們在互聯(lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的成都網(wǎng)站制作、成都網(wǎng)站設(shè)計、網(wǎng)絡(luò)營銷經(jīng)驗,集策劃、開發(fā)、設(shè)計、營銷、管理等多方位專業(yè)化運作于一體,具備承接不同規(guī)模與類型的建設(shè)項目的能力。
import?java.util.Vector;
public?class?JavaMain?{
static?private?final?int?ARRAY_SIZE?=?6;
public?static?void?main(String[]?args)?{
VectorInteger?theVector?=?new?VectorInteger();
for?(int?cEachItem?=?0;?cEachItem??ARRAY_SIZE;?cEachItem++)?{
theVector.add(cEachItem);
}
showVector(theVector);
theVector.removeElementAt(3);
showVector(theVector);
}
public?static?void?showVector(VectorInteger?theVector)?{
if?(theVector.isEmpty())?{
System.out.println("theVector?is?empty.");
return;
}
for?(int?loop?=?0;?loop??theVector.size();?loop++)?{
System.out.print(theVector.get(loop));
System.out.print(",?");
}
System.out.println();
}
}
0, 1, 2, 3, 4, 5,
0, 1, 2, 4, 5,
你可以直接用Eclipse或者NetBeans的圖形界面,之后添加組件的響應(yīng)就可以啦,很方便的。。。。
一般來說.比較常使用的是?JTextArea文本框的無參數(shù)構(gòu)造器進(jìn)行構(gòu)造
然后可以使用setText("文字")來設(shè)置文本,或者getText()方法來獲得文本
JTextArea?jta?=?new?JTextArea();
jta.setText("你好啊");
String?txt?=?jta.getText();
具體例子
import?java.awt.*;
import?java.awt.event.*;
import?java.io.*;
import?javax.swing.*;
public?class?ReadBook?extends?JFrame{
JTextArea?jta;
JTextField?jtf;
JButton?jb;
public?ReadBook(){
jta?=?new?JTextArea();
jtf?=?new?JTextField(30);
jtf.setText("文件路徑如c:\\ab.txt");
jb?=?new?JButton("顯示文字");
JPanel?jp?=?new?JPanel();
jp.add(jtf);
jp.add(jb);
add(jta);
add(jp,BorderLayout.SOUTH);
setBounds(500,?100,?500,?380);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
jb.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
String?path?=?jtf.getText();
File?f=?new?File(path);
if(f.exists()!f.isDirectory()){//存在該文件且不是文件夾
try?{
StringBuilder?sb?=?new?StringBuilder();
BufferedReader?br?=?new?BufferedReader(new?FileReader(f));
String?temp;
while((temp=br.readLine())!=null){
sb.append(temp+"\n");
}
br.close();
jta.setText(sb.toString());//setText方法可以給jta添加文字
}?catch?(Exception?e1)?{
e1.printStackTrace();
}
}else{
jtf.setText("路徑錯誤.重新填寫");
}
}
});
}
public?static?void?main(String[]?args)?{
new?ReadBook();
}
}
效果
為什么不首先設(shè)置一個歡迎界面呢?在歡迎界面上定義一個開始考試按鈕,點擊按鈕跳轉(zhuǎn)到考題頁面,把所有的考題設(shè)置在一張表單里面,考題無非設(shè)置成一些單選或者多選的按鈕。然后每個題目就是一個屬性,獲取驗證,對了多少分,錯誤沒分,然后統(tǒng)計下分?jǐn)?shù)不就完了。希望對你設(shè)計有幫助哈。
你為什么非要只給正確選項的radiobutton設(shè)置listener呢? 你就在你提交button觸發(fā)事件的時候, 同時去檢測上面所有的正確答案的radiobutton 是否被選中不就完了?
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class vv extends JDialog {
private static final long serialVersionUID = 1L;
private JLabel l_Id = new JLabel("登陸賬戶", JLabel.CENTER);
private JLabel l_pw = new JLabel("登陸密碼", JLabel.CENTER);
private JTextField t_Id = new JTextField(10);
private JPasswordField t_pw = new JPasswordField(10);
private JButton btnLogin;
private JButton btnClose;
public vv() {
super();
setResizable(false);
getContentPane().setBackground(new Color(225, 225, 225));
getContentPane().setLayout(null);
initialize();
}
protected void initialize() {
setTitle("系統(tǒng)登錄");
l_Id.setBounds(48, 43, 53, 25);
t_Id.setBounds(110, 43, 150, 25);
l_pw.setBounds(48, 93, 53, 25);
t_pw.setBounds(110, 93, 150, 25);
getContentPane().add(l_Id);
getContentPane().add(l_pw);
getContentPane().add(t_Id);
getContentPane().add(t_pw);
btnLogin = new JButton();
btnLogin.setText("登 錄");
btnLogin.setBounds(70, 142, 85, 28);
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addBtnLoginActionListener();
}
});
getContentPane().add(btnLogin);
btnClose = new JButton();
btnClose.setText("關(guān) 閉");
btnClose.setBounds(175, 142, 85, 28);
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
System.exit(-1);
}
});
getContentPane().add(btnClose);
}
private void addBtnLoginActionListener() {
String user = t_Id.getText();
String password = new String(t_pw.getPassword());
if (user.equals("")) {
JOptionPane.showMessageDialog(this, "帳號不可為空", "Caution",
JOptionPane.WARNING_MESSAGE);
return;
}
if (password.equals("")) {
JOptionPane.showMessageDialog(this, "密碼不可為空", "Caution",
JOptionPane.WARNING_MESSAGE);
return;
}
String sql = "select * FROM login WHERE id = '" + user + "' and pw = '"
+ password + "'";
boolean success = false;
// TODO:數(shù)據(jù)校驗 success = executeQuery(sql);
if (success) {
// TODO: 如果數(shù)據(jù)校驗成功 顯示主界面 并關(guān)閉登錄界面
JOptionPane.showMessageDialog(this, "成功登錄", "提示",
JOptionPane.INFORMATION_MESSAGE);
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "帳號或密碼錯誤!", "警告",
JOptionPane.WARNING_MESSAGE);
t_pw.requestFocus(); // 密碼框選中
}
}
public Dimension getPreferredSize() {
return new Dimension(320, 170);
}
public void show() {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screen = tk.getScreenSize();
Dimension d = getSize();
this.setLocation((screen.width - d.width) / 2,
(screen.height - d.height) / 2);
// 輸入密碼后回車相當(dāng)于點擊了登錄按鈕
getRootPane().setDefaultButton(btnLogin);
t_pw.requestFocus();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(300, 220);
super.show();
}
public static void main(String[] args) {
vv loginFrame = new vv();
loginFrame.setVisible(true);
}
}
希望對你有幫助