1. java中的desktopPane类起什么作用
Desktop Pane是一种特殊的Layered pane,用来建立虚拟桌面(Vitual
Desktop).它可以显示并管理众多Internal Frame之间的层次关系。以下是JDesktopPane的类层次结构图;
类层次结构图:
java.lang.Object
--java.awt.Component
--java.awt.Container
--javax.swing.JComponent
--javax.swing.JLayeredPane
--javax.swing.JDesktop
强烈建议看看这里:
http://blog.csdn.net/heimaoxiaozi/archive/2007/01/31/1499256.aspx
2. 题目如图
按照你的要求利用JAVA AWT组件编写的Java程序如下:
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Frame;
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 AFD extends Frame implements ActionListener{
Choice c=new Choice();
TextField tf1=new TextField(5);
TextField tf2=new TextField(5);
TextField tf3=new TextField(5);
Button b=new Button("=");
Panel p=new Panel();
AFD(){
super("四则运算");
c.add("+");c.add("-");c.add("*");c.add("/");
孙瞎p.setBackground(Color.CYAN);
b.setBackground(Color.CYAN);
tf3.setEditable(false);
p.add(tf1);p.add(c);p.add(tf2);p.add(b);p.add(tf3);
b.addActionListener(this);
add(p);
setSize(300, 100);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}});
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==b){
double d1=Double.parseDouble(tf1.getText().trim());
double d2=Double.parseDouble(tf2.getText().trim());
String s=c.getSelectedItem();
if(s.equals("+")){
汪仔tf3.setText(String.valueOf(d1+d2));
}else if(s.equals("-")){
tf3.setText(String.valueOf(d1-d2));
}else if(s.equals("*")){
tf3.setText(String.valueOf(d1*d2));
}else if(s.equals("/")){
tf3.setText(String.valueOf(d1/d2));
}
}
}
public static void main(String[] args) {
困凯汪new AFD();
}
}
运行结果:
3. java桌面数据库应用程序生成jar文件后,在本机运行正常,但在其他机器上异常
1 检查SQL SERVER 是否允许远程访问.具体步骤:
1)打开"企业管理器",打开控制台根目录>SQL Server 组>数据库
2)在相应灶轿"数据库"上单击右键,选择"属性"
3)选择"连接"选项卡,检查"远程服务器连接"下,RPC服务是否选择.
2 使用telnet IP地址 1433,系统是否提示连接出错,如系统提示出错
检查是否防火墙屏蔽了SQL SERVER 或 java IDE 的网络访问端口
如果是,关闭防火墙,重新启动SQL SERVER和java IDE,进行测试,
如果系统仍提李辩液示上述错误,尝试下列步骤
3 检查SQL SERVER 端口号及是否启用了TCP/IP协议,具体步骤:
1)打开"企业管理器",打开控制台根目录>SQL Server 组>数据库
2)在相应"数据库"上单击右键,选择"属性"
3)选择"常规"选项卡,点击"网络配置",如启用的协议中无"哪物TCP/IP协议"将其加入
4)选择"TCP/IP协议",点击"属性",检查其端口号是否为1433
5)如端口号为1433将其修改为其它端口号,修改jdbc连接语句,将端口号同样改为新启用的端口号,如jdbc:microsoft:sqlserver://server_name:1400(假设新端口号为 1400)
4. java 窗口背景图片无法加载
你好,你这种情况我曾经遇过。
不能显示背景图片的原因是它宏谈被其他组件挡住了,主要原因是布局的问题,你设置为空,JFrame就不会帮你布局,你要特别注意添加组件的顺序,我特意为你写了一个符合你要求的JFrame,跟你那个完全一样,只要你的图片跟我的图片一样就行了。
解决办法(参照如下代码):
/*
*Tochangethistemplate,chooseTools|Templates
*andopenthetemplateintheeditor.
*/
/*
*MyIndex.java
*
*Createdon2011-5-8,1:00:26
*/
packagetestjframebgcolor;
/**
*
*@authorStudy
*/
.swing.JFrame{
/**CreatesnewformMyIndex*/
publicMyIndex(){
initComponents();
}
/**
*initializetheform.
冲腊*WARNING:DoNOTmodifythiscode.Thecontentofthismethodis
*.
*/
@SuppressWarnings("unchecked")
//<editor-folddefaultstate="collapsed"desc="GeneratedCode">
privatevoidinitComponents(){
lb1=newjavax.swing.JLabel();
Enter=newjavax.swing.JButton();
Login=newjavax.swing.JButton();
tf2=newjavax.swing.JTextField();
tf1=newjavax.swing.JTextField();
lb2=newjavax.swing.JLabel();
imgLabel=newjavax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setName("Form");//NOI18N
getContentPane().setLayout(null);
org.jdesktop.application.ResourceMapresourceMap=org.jdesktop.application.Application.getInstance(testjframebgcolor.TestJFrameBGColorApp.class).getContext().getResourceMap(MyIndex.class);
lb1.setText(resourceMap.getString("lb1.text"));//NOI18N
lb1.setName("lb1");//NOI18N
蔽判碰getContentPane().add(lb1);
lb1.setBounds(75,85,60,20);
Enter.setText(resourceMap.getString("Enter.text"));//NOI18N
Enter.setName("Enter");//NOI18N
getContentPane().add(Enter);
Enter.setBounds(230,185,65,20);
Login.setText(resourceMap.getString("Login.text"));//NOI18N
Login.setName("Login");//NOI18N
getContentPane().add(Login);
Login.setBounds(100,185,65,20);
tf2.setName("tf2");//NOI18N
getContentPane().add(tf2);
tf2.setBounds(165,125,120,20);
tf1.setText(resourceMap.getString("tf1.text"));//NOI18N
tf1.setName("tf1");//NOI18N
getContentPane().add(tf1);
tf1.setBounds(165,85,120,20);
lb2.setText(resourceMap.getString("lb2.text"));//NOI18N
lb2.setName("lb2");//NOI18N
getContentPane().add(lb2);
lb2.setBounds(75,125,60,20);
imgLabel.setIcon(resourceMap.getIcon("imgLabel.icon"));//NOI18N
imgLabel.setText(resourceMap.getString("imgLabel.text"));//NOI18N
imgLabel.setName("imgLabel");//NOI18N
imgLabel.setOpaque(true);
getContentPane().add(imgLabel);
imgLabel.setBounds(0,0,400,330);
setBounds(450,250,400,330);
}//</editor-fold>
/**
*@
*/
publicstaticvoidmain(Stringargs[]){
java.awt.EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
newMyIndex().setVisible(true);
}
});
}
//Variablesdeclaration-donotmodify
protectedjavax.swing.JButtonEnter;
protectedjavax.swing.JButtonLogin;
protectedjavax.swing.JLabelimgLabel;
protectedjavax.swing.JLabellb1;
protectedjavax.swing.JLabellb2;
protectedjavax.swing.JTextFieldtf1;
protectedjavax.swing.JTextFieldtf2;
//Endofvariablesdeclaration
}
5. java JDIC 无法使用
有两个DLL文件要放在环境变量PATH里面。或者放到当前运费的目录中。
可以放在SYSTEM32或者JRE bin目录里。
6. java编程问题:在swing面板中展示某一URL地址的页面
你可以写个简单的JAVA浏览器
这有个源码
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.jdesktop.jdic.browser.IWebBrowser;
import org.jdesktop.jdic.browser.WebBrowser;
import org.jdesktop.jdic.browser.WebBrowserEvent;
import org.jdesktop.jdic.browser.WebBrowserListenerAdapter;
/**
*
* @author hadeslee
*/
public class Test1 extends JPanel implements ActionListener {
private JTextField input;
private JButton go;
private IWebBrowser web;
public Test1() {
super(new BorderLayout());
initWindow();
}
private void initWindow() {
try {
web = new WebBrowser();
web.addWebBrowserListener(new MyListener());
go = new JButton("转到");
input = new JTextField();
JPanel up = new JPanel(new BorderLayout());
up.add(input, BorderLayout.CENTER);
up.add(go, BorderLayout.EAST);
this.add(up, BorderLayout.NORTH);
this.add(web.asComponent(), BorderLayout.CENTER);
input.addActionListener(this);
go.addActionListener(this);
} catch (Exception ex) {
Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex);
}
JFrame jf = new JFrame("JAVA浏览器");
jf.add(this, BorderLayout.CENTER);
jf.setSize(500, 300);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae) {
doOpen();
}
private void doOpen() {
try {
String text = input.getText();
if (text == null || text.equals("")) {
return;
}
if (!text.toLowerCase().startsWith("http://")) {
text = "http://" + text;
}
web.setURL(new URL(text));
} catch (MalformedURLException ex) {
Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
new Test1();
}
private class MyListener extends WebBrowserListenerAdapter {
private MyListener() {}
@Override
public void documentCompleted(WebBrowserEvent arg0) {
System.out.println("文档下载完。。。");
web.executeScript("alert('文档下载完毕!')");
// web.setContent("<html><H1>Hello world!!<H1>" +
// "<a href=http://www.google.cn>点我</a></html>");
// web.removeWebBrowserListener(this);
}
}
}
7. Java的“静态库链接”
Java的库组织方式就是 动态链接 的 从一个Java的jar包运行有可能要接一堆classpath就知道 和基于静态链接的C语言要实现动态链接要做额外的事情相似 Java要想实现类似C的静态链接也要做很多额外的事
用类似Fat Jar的方法 把所有的依赖库打包的最后的库中 其实不是静态链接——C的静态链接只把需要的代码复制过来 不是眉毛胡子一把抓 按说 以Java的思想 静态链接不是很必要 因此也就没有原生支持 但实践和理论毕竟差距很远 不是每个库都是标准库 假设你从别人的库中引用了几个类 为了支持你的程序 你必须提供别人的库(假设这个库并不流塌厅行) 再假设你自己写了一个库稿衫耐 以后再开发类似的程序就从库中派生 当然你不想把所有的代码都发行出去 这个问题在Netbeans上更为明显 Netbeans提供了一个swing框架 用起来当然很方便 但是当发行程序的时候你就会发现 Netbeans很负责任的把依赖库放到发行目录的lib下 居然有将近 M (禁掉粗口) 光写一个窗口就要 M!
ProGuard不光是个混淆器 它也能解决静态链接的问题 用它自己的话说是 It detects and removes unused classes fields methods and attributes 下面给出的是在Netbeans中用的Ant脚本 修改项目的build xml 添加
<target name= post jar > <taskdef resource= proguard/ant/task properties classpath= ${libs proguard classpath} /> <file src= ${dist jar} dest= ${dist dir}/pre jar /> <proguard warn= false obfuscate= false > <libraryjar path= ${java home}/lib/rt jar />键春 <injar path= ${javac classpath} filter= !META INF/MANIFEST MF /> <injar path= ${dist dir}/pre jar /> <outjar path= ${dist jar} /> <keep name= ${main class} > <method name= main /> </keep> <keep name= jdesktop bean *** inding ext BeanAdapterProvider /> <keepclasseswithmembernames> <method name= getServiceNames /> </keepclasseswithmembernames> <keepclasseswithmembernames> <method name= addPropertyChangeListener /> </keepclasseswithmembernames> </proguard> </target>
lishixin/Article/program/Java/hx/201311/26546
8. java 中string里replaceall中使用正则出错的疑问!
另一位给出的是错误的。
java对于正则一块,只要是含/n的正则表达式,处理时,匹配出的字符蔽毁串长时,就会出错。
可以,先只区配/n,比如替换成其它字串。"|||"
然后,再用replaceall,进行处理,把需要删除的删除掉。
最后,再用替换戚搭,将"|||"替换回"\n" ,即可。高并拿
9. java窗口背景颜色怎么设定用setBackground()好像不行,请大侠指教!
你好!
首先,你说的Java窗口是指JFrame或者Frame
其次,你说的窗口背景颜色是指直接调用JFrame或者Frame的setBackground(Colorcolor)方法设置后显示出来的颜色。其实,你的想法是正确的,但是我想提醒你的是,你没搞明白JFrame的显示机制。在你直接调用这个方法后,你的确设置了背景颜色,而你看到的却不是直接的JFrame或者Frame,而是JFrame.getContentPane().而JFrame上的contentPane默认是Color.WHITE的,所以,无论你对JFrame或者Frame怎么设置背景颜色,你看到的都只是contentPane.
最后,讲解决办法:
办法A:在完成初始化,调用getContentPane()方法得到一个contentPane容器,然后将其设置为不可见,即setVisible(false)。这样,你就可以看到JFrame的庐山真面貌啦!
核心代码this.getContentPane().setVisible(false);//得到contentPane容器,设置为不可见
实例完整代码如下:
/*
*TestJFrameBGColor.java
*
*Createdon2011-5-8,0:21:20
*/
packagetestjframebgcolor;
importjava.awt.Color;
/**
*
*@author叶科良
*/
.swing.JFrame{
/***/
publicTestJFrameBGColor(){
initComponents();
this.getContentPane().setVisible(false);//得到contentPane容器,设置为不可见
}
/**
*initializetheform.
*WARNING:DoNOTmodifythiscode.Thecontentofthismethodis
*.
*/
@SuppressWarnings("unchecked")
//<editor-folddefaultstate="collapsed"desc="GeneratedCode">
privatevoidinitComponents(){
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
org.jdesktop.application.ResourceMapresourceMap=org.jdesktop.application.Application.getInstance(testjframebgcolor.TestJFrameBGColorApp.class).getContext().getResourceMap(TestJFrameBGColor.class);
setBackground(resourceMap.getColor("Form.background"));//NOI18N
setName("Form");//NOI18N
javax.swing.GroupLayoutlayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,400,Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,300,Short.MAX_VALUE)
);
pack();
}//</editor-fold>
/**
*@
*/
publicstaticvoidmain(Stringargs[]){
java.awt.EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
newTestJFrameBGColor().setVisible(true);
}
});
}
//Variablesdeclaration-donotmodify
//Endofvariablesdeclaration
}
方法B:将contentPane的颜色设置为你想要的颜色,而不是对JFrame本身设置,
核心代码:this.getContentPane().setBackground(Color.red);//设置contentPane为红色
将核心代码替换方法A核心代码即可实现
方法C:为JFrame添加一个Panel或者JLabel等其他组件,设置其颜色为你想要的颜色,然后将其覆盖JFrame窗口即可