1.能设背景图片吗?
2.能改 JTextPane 等 的字体属性吗? 我就找到个 setForeground(Color.xx) 不能改 字体 大小 形式等吗?
3.我用鼠标点击事件 改变的值 怎么不立即传递啊,比如选项1 需要a=true 点选项2后 a=true 但此时 点选项1 他还提示a 不等于true 需要 在点次选项2才可以 这是为什么
2.能改 JTextPane 等 的字体属性吗? 我就找到个 setForeground(Color.xx) 不能改 字体 大小 形式等吗?
3.我用鼠标点击事件 改变的值 怎么不立即传递啊,比如选项1 需要a=true 点选项2后 a=true 但此时 点选项1 他还提示a 不等于true 需要 在点次选项2才可以 这是为什么
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;public class flag extends JFrame implements ActionListener {
private boolean landing = false; // 标志登陆否 private JTextPane textarea = new JTextPane();
private JPanel panel1 = new JPanel();
private JLabel label = new JLabel();
private JDialog colorDialog = new JDialog();
private JFileChooser filechooser = new JFileChooser();
private ButtonGroup buttongruop1 = new ButtonGroup();
private ButtonGroup buttongruop2 = new ButtonGroup();
private ButtonGroup buttongruop3 = new ButtonGroup(); private JMenuBar bar = new JMenuBar(); // 工具条 private JMenu fileMenu = new JMenu("File");
private JMenu editMenu = new JMenu("Edit");
private JMenu quitMenu = new JMenu("Quit");
private JMenu landMenu = new JMenu("Land"); private JMenuItem open = new JMenuItem("open");
private JMenuItem quit = new JMenuItem("quit");
private JMenuItem land = new JMenuItem("land");
private JMenuItem editColor = new JMenuItem("BGcolor");
private JMenuItem fontColor = new JMenuItem("FontColor"); private JRadioButton red = new JRadioButton("红");
private JRadioButton white = new JRadioButton("白");
private JRadioButton green = new JRadioButton("绿"); // 字体属性
private JDialog font_set = new JDialog();
private JPanel fontNamePanel = new JPanel();
private JPanel fontSizePanel = new JPanel();
private JPanel fontStylePanel = new JPanel();
private JRadioButton font_red = new JRadioButton("红色");
private JRadioButton font_black = new JRadioButton("黑色");
private JRadioButton font_white = new JRadioButton("白色");
private JRadioButton font_12 = new JRadioButton("12");
private JRadioButton font_15 = new JRadioButton("15");
private JRadioButton font_20 = new JRadioButton("20");
private JCheckBox font_BOLD = new JCheckBox("粗体");
private JCheckBox font_ITALIC = new JCheckBox("斜体");
private int fontSize;
private int fontStyle;
private int fontName; private JDialog lander = new JDialog(); // 新建登陆对话框
private JLabel lableName = new JLabel("name:");
private JLabel lablePwd = new JLabel("password:");
private JTextField txname = new JTextField(); // 文本区域
private JPasswordField txpsd = new JPasswordField(); // 密码区域
private JButton over = new JButton("ok"); public flag() {
setTitle("文本编辑器");
/**
* ImageIcon image = new
* ImageIcon("E:\\java\\h\\src\\Textarea\\ha.JPG");
* label.setIcon(image); //图象添加
*/ JScrollPane jscroll = new JScrollPane(textarea); add(jscroll);
setJMenuBar(bar); // 设置菜单
bar.add(fileMenu);
bar.add(editMenu);
bar.add(quitMenu);
bar.add(landMenu);
fileMenu.add(open);
quitMenu.add(quit);
landMenu.add(land);
editMenu.add(editColor);
editMenu.add(fontColor); // 设置登陆框样式 lander.setLayout(new GridLayout(3, 1)); // 登陆框
lander.setTitle("lander");
lander.setLocation(400, 500);
lander.setSize(200, 100);
lander.add(lableName);
lander.add(txname);
lander.add(lablePwd);
lander.add(txpsd);
// lander.add("north",panel1);
lander.add(over); // 设置颜色对话框
colorDialog.setLayout(new GridLayout(0, 1));
buttongruop1.add(red);
buttongruop1.add(white);
buttongruop1.add(green);
colorDialog.add(red);
colorDialog.add(white);
colorDialog.add(green);
colorDialog.setLocation(400, 500);
colorDialog.setSize(50, 100); // 设置字体对话框
fontNamePanel.setBorder(new TitledBorder("color"));
fontSizePanel.setBorder(new TitledBorder("size"));
fontStylePanel.setBorder(new TitledBorder("style"));
font_set.add(fontNamePanel);
font_set.add(fontStylePanel);
font_set.add(fontSizePanel);
fontNamePanel.add(font_black);
fontNamePanel.add(font_white);
fontNamePanel.add(font_red);
buttongruop2.add(font_black);
buttongruop2.add(font_white);
buttongruop2.add(font_red);
fontSizePanel.add(font_12);
fontSizePanel.add(font_15);
fontSizePanel.add(font_20);
fontStylePanel.add(font_BOLD);
fontStylePanel.add(font_ITALIC);
buttongruop3.add(font_12);
buttongruop3.add(font_15);
buttongruop3.add(font_20); font_set.setLayout(new GridLayout(0, 1));
font_set.setSize(300, 200);
font_set.setLocation(400, 500); setLocation(400, 300); // 设置主框位置
setSize(400, 200);
setVisible(true); open.addActionListener(this); // 单击监听
quit.addActionListener(this);
land.addActionListener(this);
editColor.addActionListener(this);
over.addActionListener(this);
red.addActionListener(this);
white.addActionListener(this);
green.addActionListener(this);
fontColor.addActionListener(this);
font_black.addActionListener(this);
font_red.addActionListener(this);
font_white.addActionListener(this);
font_12.addActionListener(this);
font_15.addActionListener(this);
font_20.addActionListener(this); } @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub if (e.getSource() == open) {
if (!landing) {
JOptionPane.showMessageDialog(null, "NO landing", "error",
JOptionPane.ERROR_MESSAGE);
} else {
int i = filechooser.showOpenDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
File f = filechooser.getSelectedFile();
try {
InputStream is = new FileInputStream(f);
textarea.read(is, "d");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
if (e.getSource() == quit) {
if (!landing) {
JOptionPane.showMessageDialog(null, "NO landing", "",
JOptionPane.ERROR_MESSAGE);
} else
System.exit(0);
} if (e.getSource() == editColor) {
if (!landing)
JOptionPane.showMessageDialog(null, "NO landing", "",
JOptionPane.ERROR_MESSAGE);
else
colorDialog.setVisible(true);
}
if (e.getSource() == red)
textarea.setBackground(Color.red);
if (e.getSource() == white)
textarea.setBackground(Color.white);
if (e.getSource() == green)
textarea.setBackground(Color.green); if (e.getSource() == fontColor) {
if (!landing)
JOptionPane.showMessageDialog(null, "NO landing", "",
JOptionPane.ERROR_MESSAGE);
else
font_set.setVisible(true);
}
if (e.getSource() == font_red) {
textarea.setForeground(Color.red);
}
if (e.getSource() == font_black) {
textarea.setForeground(Color.black);
}
if (e.getSource() == font_white) {
textarea.setForeground(Color.white);
} if (e.getSource() == land) {
lander.setVisible(true);
if (txname.getText().equals("001") && txpsd.getText().equals("001")) {
landing = true;
}
}
if (e.getSource() == over)
lander.setVisible(false); } public static void main(String[] args) {
new flag();
}
}
为什么我这里 这样子
font_set.add(fontNamePanel);
font_set.add(fontStylePanel);
font_set.add(fontSizePanel);和font_set.add(fontNamePanel,"South");
font_set.add(fontStylePanel,"North");
font_set.add(fontSizePanel,"Center");结果没任何不同?位置就没变化,帮下忙吧,看下到底为什么...谢谢了
这个不会是你想要的。你这样:
font_set.add(fontNamePanel,BorderLayout.SOUTH);
这样才对。其他两个也一样,引用BorderLayout的常量BorderLayout.CENTER和
BorderLayout.NORTH
JTextPane 有setFont方法,继承自JComponent设置背景,你指的是给什么加背景?
你看这段:
if (e.getSource() == land) {
lander.setVisible(true);
if (txname.getText().equals("001") && txpsd.getText().equals("001")) {
landing = true;
}
}
首先,这是打开登陆对话框,那么,这个时候你应该至少增加一个
lander.setModal(true);
在lander.setVisible(true);之前。因为如果不是模式窗口,不会发生阻塞,代码也就一直运行下去了。也就是说根本还不等你输入就已经开始进行验证了。这就是你所说的第一次进不去。而第二次,你的对话框没有销毁,也没有清空,保留了你上一次输入的内容,此时才队上一次输入进行验证。个人建议,将验证工作在触发你的那个over按钮时候再进行。当然,通常的登陆,模式状态应该是必须的。
你的代码我运行过,我不会不加验证随便乱说的。你试验一下吧。
-----------------------------------------------------------
一样的道理。我想也许你应该用Debug跟踪一下你的代码。
因为JTextPane是支持HTML的