2024-03-19 15:02:12 | 我爱编程网
你没有配环境变量
在我的电脑右建--》属性---》高级--》环境变量
添加个classpath值设置为“.”
还有JAVA_HOME 值为“你JDK的安装目录”
还有个就是path里面添加“%JAVA_HOME%\bin”
一个ubuntu下运行java程序的实例
首先创建.java文件
进入某个目录,
然后创建一个.java文件:vim HelloWorld.java
在里面输入代码:
public class HelloWorld{
public static void main(String args[]){
System.out.println("Hello World in Ubuntu!");
}
}
保存并退出vim::x,回车。
编译HelloWorld.java:javac HelloWorld.java 得到HelloWorld.class文件
运行HelloWorld:sudo java HelloWorld
结果输出:Hello World in Ubuntu!
如果只是一些文本要显示,可以在上面加个JTextArea,JTextField之类的组件,用这些组件的对象调用setText(" ")方法(括号里是字符串)来显示文本
比如:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/** 文本区域
*
* @author Administrator
*
*/
public class TextArea extends JFrame {
private static final long serialVersionUID = 2306597749734227012L;
private JButton b1 = new JButton("Add Data"), b2 = new JButton("Clear Data");
private JTextArea text = new JTextArea(20, 40);
private Map
new HashMap
public TextArea() {
// Use up all the data:
map.put("a", "1");
map.put("b", "2");
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
for(@SuppressWarnings("rawtypes") Map.Entry me : map.entrySet()){
text.append(me.getKey()+": "+me.getValue()+"\n");
}
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
text.setText("");
}
});我爱编程网
setLayout(new FlowLayout());
add(new JScrollPane(text));
add(b1);
add(b2);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
GUItools.SwingConsole.run(new TextArea(), 475, 475);
}
}
JTextField和这差不多,只不过是显示单行的。
如果要显示其他字体的,可以用HTML格式,如下:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
/** Swing 组件上的HTML
*
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class HTMLButton extends JFrame {
private JButton b = new JButton(
""+
"
// 必须使文本以 "" 标记开始。
public HTMLButton() {
// TODO Auto-generated constructor stub
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
add(new JLabel(""+"Kapow!"));
// Force a re-layout to include the new label:
validate();
}
});
setLayout(new FlowLayout());
add(b);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
GUItools.SwingConsole.run(new HTMLButton(), 200, 500);
}
}
2024-03-02 02:58:47
2024-01-01 09:58:48
2024-01-26 10:37:53
2024-03-04 07:43:42
2024-01-17 23:29:33
2024-01-01 04:24:22