求使用java里与SWING的视窗界面有关的知识的GUI程序 代码

Python022

求使用java里与SWING的视窗界面有关的知识的GUI程序 代码,第1张

package testGUI

import java.awt.BorderLayout

import java.awt.Button

import java.awt.Color

import java.awt.Frame

import java.awt.GridLayout

import java.awt.Label

import java.awt.Panel

import java.awt.TextField

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.awt.event.WindowAdapter

import java.awt.event.WindowEvent

public class Add {

public static void main (String []args){

new TFrame("java加法")

}

}

class TFrame extends Frame {

TextField tf1,tf2,tf3

TFrame (String s){

super(s)

this.setLocation(200, 200)

this.setLayout(new BorderLayout())

Panel p = new Panel(new GridLayout(1,5,1,20))

tf1 = new TextField("0",5)

tf2 = new TextField("0",5)

tf3 = new TextField("0",5)

tf1.setBackground(Color.green)

tf2.setBackground(Color.green)

tf3.setBackground(Color.green)

Label lb = new Label("+",Label.CENTER)

lb.setBackground(Color.lightGray)

Button bt = new Button("=")

bt.setBackground(Color.gray)

bt.addActionListener(new Action(this))

p.add(tf1)p.add(lb)p.add(tf2)p.add(bt)p.add(tf3)

add(p,BorderLayout.NORTH)

this.addWindowListener(new Close())

pack()

setVisible(true)

}

}

class Close extends WindowAdapter {

@Override

public void windowClosing(WindowEvent e) {

System.exit(0)

}

}

class Action implements ActionListener {

TFrame f = null

Action (TFrame f){

this.f = f

}

public void actionPerformed (ActionEvent e){

int n1 = Integer.parseInt(f.tf1.getText())

int n2 = Integer.parseInt(f.tf2.getText())

f.tf3.setText(String.valueOf("" + (n1+n2)))

}

}

import javax.swing.*

import java.awt.event.*

import java.io.*

public class Demo03 extends JFrame implements ActionListener{

// 半角字库

private final String _half = "1234567890!@#$%^&*()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_=+\\|[]:'\",<.>/?()【】《》“”"

// 全角字库

private final char[] _full = new char[] { '1', '2', '3', '4', '5',

'6', '7', '8', '9', '0', '!', '@', '#', '$', '%', '^', '&', '*',

'(', ')', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',

'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',

'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',

'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',

'Y', 'Z', '-', '_', '=', '+', '\', '|', '【', '】', ';', ':', '\'',

'\'', ',', '〈', '。', '〉', '/', '?', '(', ')', '【', '】', '《', '》',

'『', '』' }

private int row = 14

private int col = 23

private JTextArea jta = new JTextArea()

private JFileChooser jfc = new JFileChooser()

private JMenuItem jmiOpen = new JMenuItem("打开")

public Demo03(){

this.setDefaultCloseOperation(3)

this.add(new JScrollPane(jta))

JMenuBar jmb = new JMenuBar()

this.setJMenuBar(jmb)

JMenu jm1 = new JMenu("文件")

jmb.add(jm1)

jm1.add(jmiOpen)

jmiOpen.addActionListener(this)

this.setSize(300,300)

this.setResizable(false)

this.setVisible(true)

}

public void actionPerformed(ActionEvent e) {

if(e.getSource().equals(jmiOpen)){

int i = jfc.showOpenDialog(this)

System.out.println (i)

if(i==0){

this.setJta(this.getTable(this.getFile(jfc.getSelectedFile())))

}

}

}

private char[] getFile(File file){

try {

String s = ""

StringBuilder builder = new StringBuilder()

BufferedReader br = new BufferedReader(new FileReader(file))

while ((s = br.readLine())!= null) {

builder.append(s)

}

return builder.toString().toCharArray()

}

catch (Exception ex) {

ex.printStackTrace()

}

return null

}

private void setJta(char[][] chars){

StringBuilder builder = new StringBuilder()

for (int i = 0i<rowi++) {

for (int j = 0j<colj++) {

builder.append(chars[i][j])

}

builder.append("\n")

}

jta.setText(builder.toString())

}

private char[][] getTable(char[] chars){

char[][] shuPai = new char[row][col]

int k = 0

for (int j = col-1j>=0j-- ) {

for (int i = 0i<rowi++,k++) {

if(k<chars.length)

shuPai[i][j] = toChar(chars[k])

else

shuPai[i][j] = ' '

}

}

return shuPai

}

private char toChar(char ch) {

Character nch

int pos = _half.indexOf(ch)

if (pos != -1) {

nch = new Character(_full[pos])

} else {

nch = new Character(ch)

}

return nch.charValue()

}

public static void main(String args[]){

new Demo03()

}

}