Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

how to import javax.swing in android studio

I have just setup Android Studio and started a simple application. Well started is an over statement, I got stuck on first few lines and am unable to import JFrame into android studio.

I have the latest SDK installed aling with LibGDX. I am still not able to setup JFrame. I have searched the net/youtube and no solutions have been found.

I can see the javax and swing in my external libraries but just cannot import.

Any ideas what I am doing wrong or not doing?

I am not looking for a "how to tutorial", I just a pointer where I should go hunting for the answer.

wow, not huge amount of response.

Please advise if I have asked a stupid question or difficult question.

public hungryDog() {

        JFrame jframe = new JFrame(); 
        Timer timer = new Timer(20, this); 

                renderer = new Renderer(); 
        rand = new Random(); 


        jframe.add(renderer); 
        jframe.setTitle("Hungry Dog"); 
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        jframe.setSize(WIDTH, HEIGHT); 
        jframe.addMouseListener(this); 
        jframe.addKeyListener(this); 
        jframe.setResizable(false); 
        jframe.setVisible(true); 


        dog = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20); 
        columns = new ArrayList<Rectangle>(); 


        addColumn(true); 
        addColumn(true); 
        addColumn(true); 
        addColumn(true); 


        timer.start(); 
    } 

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I used to have the same problem; here's how to solve it. You see, we were trying to use swing lib in a totally wrong context, that is, within an Android app. As Scott Barta pointed out, Android has its own mechanisms for doing what we wanted to achieve and that's why IntelliJ didn't let us import anything that would interfere with Android API. Hence, do NOT use Android app when, say, simply learning how to code in Java or, on a more advanced level, when testing/debugging your algorithms. Instead, build a standalone Java program (yes, in Android Studio). This very topic is covered here: Can Android Studio be used to run standard Java projects? , "Tested on Android Studio 0.8.6 - 1.0.2" by idunnololz). A few clarifying remarks to this solution: 1) wnen preparing your configuration (Run | Edit Configurations...), use your new Java module name and its main class in the appropriate fields. 2) before clicking Run make sure you selected same configuration.

Incidentally, there indeed IS a way to import swing library into any Android app: based on http://www.youtube.com/watch?v=fHEvI_G6UtI. Specifically, add a new line to build.gradle file for your Module:app: compile files ('<path_to_your_jdk's_rt.jar>') Like this: compile files ('C:/Program Files/Java/jdk1.8.0_31/jre/lib/rt.jar') Note single quotes and forward slashes. Then click Sync Gradle icon and enjoy the view.

In our particular case, however, the program won't run. The code itself will be clean, there'll be no errors; with swing lib well in place and connected, IntelliJ won't suspect a thing; the project WILL compile... but then we'll get a runtime error. Now your know why. On the other hand, this method works like magic when we do not interfere with Android API --- which we shouldn't in the first place.

That's all there is to it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...