java 用GUI写一个程序
我写的这个显示了一个字符串,但要求要显示两个及其以上的字符串,且有不同的格式。比如:a字符串大小为18号,b字符串大小为14号。我想了很久了……只能求助各位了。...
我写的这个显示了一个字符串,但要求要显示两个及其以上的字符串,且有不同的格式。比如:a字符串 大小为18号,b字符串 大小为14号。
我想了很久了……只能求助各位了。 展开
我想了很久了……只能求助各位了。 展开
3个回答
展开全部
使用Font类
下边是例子
---------------------------------------------------------------------------------------------
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class FontApp extends JFrame {
public FontApp() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setResizable(false);
getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("a String size 18");
lblNewLabel.setFont(new Font(null, Font.ITALIC, 18));
lblNewLabel.setBounds(12, 30, 232, 29);
getContentPane().add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("b String size 14");
lblNewLabel_1.setFont(new Font(null, Font.BOLD, 14));
lblNewLabel_1.setBounds(12, 97, 232, 29);
getContentPane().add(lblNewLabel_1);
setVisible(true);
}
public static void main(String[] args) {
new FontApp();
}
}
下边是例子
---------------------------------------------------------------------------------------------
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class FontApp extends JFrame {
public FontApp() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setResizable(false);
getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("a String size 18");
lblNewLabel.setFont(new Font(null, Font.ITALIC, 18));
lblNewLabel.setBounds(12, 30, 232, 29);
getContentPane().add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("b String size 14");
lblNewLabel_1.setFont(new Font(null, Font.BOLD, 14));
lblNewLabel_1.setBounds(12, 97, 232, 29);
getContentPane().add(lblNewLabel_1);
setVisible(true);
}
public static void main(String[] args) {
new FontApp();
}
}
展开全部
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TestRect extends JFrame implements ActionListener {
JLabel jl1;
JButton jb1,jb2,jb3;
JPanel jp;
int c=10;
public static void main(String[] args){
TestRect tr = new TestRect();
}
public TestRect(){
setSize(500,500);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.blue);
setCursor(HAND_CURSOR);
jl1 = new JLabel("Hello World!");
jl1.setFont(new Font("italic",Font.ITALIC,10));
jb1 = new JButton("加大字号");
jb1.addActionListener(this);
jb2 = new JButton("减小字号");
jb2.addActionListener(this);
jb3 = new JButton("复原字号");
jb3.addActionListener(this);
jp = new JPanel();
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
setLayout(new BorderLayout());
this.add(jl1,BorderLayout.CENTER);
this.add(jp,BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb1){
c++;
setFont();
}
if(e.getSource()==jb2){
c--;
setFont();
}
if(e.getSource()==jb3){
c=10;
setFont();
}
}
public void setFont(){
Font myfont= new Font("italic",Font.ITALIC,c);
jl1.setFont(myfont);
}
}
import javax.swing.*;
import java.awt.event.*;
public class TestRect extends JFrame implements ActionListener {
JLabel jl1;
JButton jb1,jb2,jb3;
JPanel jp;
int c=10;
public static void main(String[] args){
TestRect tr = new TestRect();
}
public TestRect(){
setSize(500,500);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.blue);
setCursor(HAND_CURSOR);
jl1 = new JLabel("Hello World!");
jl1.setFont(new Font("italic",Font.ITALIC,10));
jb1 = new JButton("加大字号");
jb1.addActionListener(this);
jb2 = new JButton("减小字号");
jb2.addActionListener(this);
jb3 = new JButton("复原字号");
jb3.addActionListener(this);
jp = new JPanel();
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
setLayout(new BorderLayout());
this.add(jl1,BorderLayout.CENTER);
this.add(jp,BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb1){
c++;
setFont();
}
if(e.getSource()==jb2){
c--;
setFont();
}
if(e.getSource()==jb3){
c=10;
setFont();
}
}
public void setFont(){
Font myfont= new Font("italic",Font.ITALIC,c);
jl1.setFont(myfont);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询